The video owner has disabled playback on external websites.

This video is no longer available on YouTube.

This video cannot be played right now.

Watch on YouTube

KI-gestützte Lerntools freischalten

Registriere dich, um leistungsstarke Tools zu nutzen, die dir helfen, schneller aus jedem Video zu lernen.

Szenen-Erklärer Phrasen-Jäger Karteikarten-Review Nachsprechübung Sprachausgabe
Kostenlos registrieren
Englisch 27:59 Science & Tech

You’ve Been Underusing Dataclasses (These Tricks Are Wild)

Late Night with Seth Meyers · 22,921 Aufrufe · Hinzugefügt vor 1 Monat

Untertitel (688 Segmente)

00:00

Do you see that in it far thingy there?

00:02

Do you know what that actually is? It

00:04

looks like a field, but it's actually

00:06

something else. I'll explain later on.

00:09

Today, I'm going to show you seven

00:11

interesting, sometimes even weird things

00:13

you can do with Python data classes.

00:16

Now, if we look at data class, they were

00:18

designed to make plain data types of

00:21

classes a bit easier to deal with. They

00:24

generate a bunch of boiler plate like an

00:26

initializer, a wrapper, a comparison,

00:29

and a few other things as well. But

00:31

under the hood, they're just normal

00:33

Python classes, which means that you can

00:36

combine them with uh class hooks,

00:38

descriptors, introspection, context

00:40

managers, and a bunch of other things as

00:42

well. Now, if you treat data classes

00:44

purely as some kind of strct, then

00:46

you're missing out on most of that

00:48

power. And that's a pattern I see all

00:50

the time in Python and software design

00:53

in general. People learn some type of

00:55

feature but not really the deeper design

00:57

ideas behind it. That's also why I'm

01:00

working on a new program called software

01:02

design mastery. This is way more than

01:04

just another online course. I can't say

01:06

too much about it yet, but it's going to

01:08

go deep into design, way beyond what you

01:11

see here on my YouTube videos. It's the

01:13

most comprehensive thing I've ever

01:15

created. If you want to be the first to

01:17

know when the program opens, make sure

01:19

you join the waiting list at

01:21

ion.code/mastery.

01:23

The first thing that I want to show you

01:24

is a data class as a singleton like

01:26

factory. Let's say you have a config

01:29

object like this and you want to have

01:32

different types of configs per

01:33

environment. Pretty standard thing

01:35

you're going to need. Right now, I've

01:38

created a data class here called config.

01:40

It has an n so I can keep track of which

01:43

environment it's part of. And I have the

01:46

actual config values. In this case,

01:48

there is just a debug flag. But of

01:50

course, you can add any types of

01:51

instance variables that you want here.

01:53

And then I create a few of these config

01:56

objects and I simply print the value of

01:58

the debugger flag. Now when I run this,

02:01

then this is what we get as a result.

02:04

Pretty simple. But let's say you want to

02:06

share this config object in different

02:08

places in your code. Now you could turn

02:10

these things into global variables and

02:13

import them in the various places or you

02:16

could do dependency injection but then

02:17

you have to inject the config

02:19

everywhere. Another thing you could do

02:20

is actually build a singleton like

02:23

factory where actually creating the

02:25

config object will return that object if

02:28

it has already been created earlier for

02:31

a particular environment. And in order

02:33

to do that of course we have to

02:34

implement the singleton pattern inside

02:36

our config class. So the first thing

02:38

that I'm going to do here is keep track

02:40

of the cache which is the instances that

02:42

we need to uh keep track of. This needs

02:45

to be a class variable because we only

02:46

want one for every instance of the data

02:49

class. The problem is if I simply

02:51

declare the cache as a dictionary of

02:54

let's say the class name to self which

02:58

is the config type that we have here.

03:00

Let me also import that from typing

03:04

like so. So now the issue is that every

03:06

instance of config is going to have this

03:08

cache and we that's not what we want. We

03:10

want a single cache. So we can store the

03:12

config instances there. Now in order to

03:14

solve this you can actually annotate

03:16

this as a class variable by importing

03:19

the class v annotation from typing

03:24

like so. And now the cache is a class

03:27

variable instead of an instance

03:29

variable. So it's going to be shared

03:31

between all the config classes. We can

03:33

initialize this to the empty dictionary.

03:36

And now the only thing we need to do is

03:38

to provide a mechanism for creating

03:41

instances of config that also take into

03:44

account the cache. Now you could try to

03:46

use the initializer for that. The

03:47

problem with that is that you have very

03:49

little control of how these values are

03:51

going to be initialized. Uh for example,

03:54

I have here an A config and a B config.

03:57

When I create the A config, I set debug

03:59

to true. I want this to be reflected in

04:03

the B config when I load it here. And

04:06

unfortunately, that's not going to

04:08

properly work with initializers. So

04:10

instead, what I'm going to do is I will

04:12

create a class method.

04:16

Let's say forn

04:19

and this is going to get class the

04:21

environment

04:23

and the debug flag.

04:26

And let's say default that is going to

04:28

be false. And this is going to return

04:33

an instance of config self. So if n

04:39

not in the cache. So since this is a

04:43

class variable, I can access it directly

04:46

like so. So if it's not there, then we

04:48

need to store it.

04:51

And we use the n value for that. And

04:54

then we create the instance of the

04:56

class.

05:01

And finally, we're going to return

05:06

the cacheed value

05:09

like so. And then instead of creating

05:11

the config objects here like so, we're

05:13

going to use the class method

05:20

like so. So now we've created a

05:22

singleton like behavior. Now this is not

05:25

ideal because every time we add extra

05:28

instance variables here we're also going

05:30

to need to add the extra parameters

05:31

here. There's some duplication of

05:33

default values. So you could optionally

05:35

just remove this to simplify that. And

05:37

of course here we also need to pass

Vollständige Untertitel im Videoplayer verfügbar

Mit Übungen trainieren

Erstelle Vokabel-, Grammatik- und Verständnisübungen aus diesem Video

Vokabeln & Grammatik Verständnisquiz IELTS-Prüfung Schreib-Übung
Registrieren zum Üben
Noch keine Kommentare. Sei der Erste, der seine Gedanken teilt!

Registriere dich, um alle Features freizuschalten

Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen

Kostenlos Sprachen lernen