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.
You’ve Been Underusing Dataclasses (These Tricks Are Wild)
Untertitel (688 Segmente)
Do you see that in it far thingy there?
Do you know what that actually is? It
looks like a field, but it's actually
something else. I'll explain later on.
Today, I'm going to show you seven
interesting, sometimes even weird things
you can do with Python data classes.
Now, if we look at data class, they were
designed to make plain data types of
classes a bit easier to deal with. They
generate a bunch of boiler plate like an
initializer, a wrapper, a comparison,
and a few other things as well. But
under the hood, they're just normal
Python classes, which means that you can
combine them with uh class hooks,
descriptors, introspection, context
managers, and a bunch of other things as
well. Now, if you treat data classes
purely as some kind of strct, then
you're missing out on most of that
power. And that's a pattern I see all
the time in Python and software design
in general. People learn some type of
feature but not really the deeper design
ideas behind it. That's also why I'm
working on a new program called software
design mastery. This is way more than
just another online course. I can't say
too much about it yet, but it's going to
go deep into design, way beyond what you
see here on my YouTube videos. It's the
most comprehensive thing I've ever
created. If you want to be the first to
know when the program opens, make sure
you join the waiting list at
ion.code/mastery.
The first thing that I want to show you
is a data class as a singleton like
factory. Let's say you have a config
object like this and you want to have
different types of configs per
environment. Pretty standard thing
you're going to need. Right now, I've
created a data class here called config.
It has an n so I can keep track of which
environment it's part of. And I have the
actual config values. In this case,
there is just a debug flag. But of
course, you can add any types of
instance variables that you want here.
And then I create a few of these config
objects and I simply print the value of
the debugger flag. Now when I run this,
then this is what we get as a result.
Pretty simple. But let's say you want to
share this config object in different
places in your code. Now you could turn
these things into global variables and
import them in the various places or you
could do dependency injection but then
you have to inject the config
everywhere. Another thing you could do
is actually build a singleton like
factory where actually creating the
config object will return that object if
it has already been created earlier for
a particular environment. And in order
to do that of course we have to
implement the singleton pattern inside
our config class. So the first thing
that I'm going to do here is keep track
of the cache which is the instances that
we need to uh keep track of. This needs
to be a class variable because we only
want one for every instance of the data
class. The problem is if I simply
declare the cache as a dictionary of
let's say the class name to self which
is the config type that we have here.
Let me also import that from typing
like so. So now the issue is that every
instance of config is going to have this
cache and we that's not what we want. We
want a single cache. So we can store the
config instances there. Now in order to
solve this you can actually annotate
this as a class variable by importing
the class v annotation from typing
like so. And now the cache is a class
variable instead of an instance
variable. So it's going to be shared
between all the config classes. We can
initialize this to the empty dictionary.
And now the only thing we need to do is
to provide a mechanism for creating
instances of config that also take into
account the cache. Now you could try to
use the initializer for that. The
problem with that is that you have very
little control of how these values are
going to be initialized. Uh for example,
I have here an A config and a B config.
When I create the A config, I set debug
to true. I want this to be reflected in
the B config when I load it here. And
unfortunately, that's not going to
properly work with initializers. So
instead, what I'm going to do is I will
create a class method.
Let's say forn
and this is going to get class the
environment
and the debug flag.
And let's say default that is going to
be false. And this is going to return
an instance of config self. So if n
not in the cache. So since this is a
class variable, I can access it directly
like so. So if it's not there, then we
need to store it.
And we use the n value for that. And
then we create the instance of the
class.
And finally, we're going to return
the cacheed value
like so. And then instead of creating
the config objects here like so, we're
going to use the class method
like so. So now we've created a
singleton like behavior. Now this is not
ideal because every time we add extra
instance variables here we're also going
to need to add the extra parameters
here. There's some duplication of
default values. So you could optionally
just remove this to simplify that. And
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
Kommentare (0)
Zum Kommentieren AnmeldenRegistriere dich, um alle Features freizuschalten
Verfolge deinen Fortschritt, speichere Vokabeln und übe mit Übungen
Interaktiver Modus
Quiz
Richtige Antwort:
Ähnliche Videos
APPLE AIRTAG 2 REVERSE ENGINEERING
Stop Mixing FastAPI with Business Logic: Fix It with Ports & Adapters
Python Properties vs Methods: The Contract You Didn’t Know You Were Making
Learning Python Feels Easy. Until It Isn’t.
You Stopped Exercising. The Gains That Stuck Around Might Surprise You.
Late Night with Seth Meyers
Quiz
Richtige Antwort:
Quizfragen erscheinen beim Anschauen des Videos
Merkhilfe
Aus diesem Video
Kostenlos Sprachen lernen