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
7 Things You Didn’t Know Dataclasses Could Do
字幕 (469 セグメント)
Most developers know the basics of data
classes in Python. You define a simple
class with some typed fields and then
Python generates the boiler plate. Here
you see an example of that. I have a
user class. It's a data class. A user
has a name and an email address. And
because it's a data class, I can now
initialize it as following. And I can
simply print it. And it's going to print
out the information in a readable way.
Here you can see the result of running
this script. But data classes can do way
more than that. So today I'll show you
seven things you probably don't know
about data classes. Each of which makes
your code safer, clearer, and easier to
maintain. And the last one tends to
surprise people. If you want to learn
how to design software from scratch,
grab my free guide at iron.code/design
guide. It walks you through my sevenstep
process for designing new software. Link
is in the video description. The first
thing that's really easy to do with data
classes is default values. For example,
if I have an active value that's
boolean, I can by default make this true
by simply assigning it a value. Then of
course, if I run this now, each of these
users are going to be active by default.
However, since it's a default value, I
can still override this and make one of
these users false like so. So then we
get this as a result. Now defaults are
sometimes a bit complicated in
particular if you're dealing with more
complicated types. Let's say you have a
tag which is a list of strings. Last a
list of strings. Now you might think you
can do this. However, this is
problematic because actually this is an
empty list and that's generated only
once when Python runs the script. And if
I run that now, you see actually data
class protects us against this by
raising a value error. So instead of
doing this, what you need to do is
define a field which you also need to
import from the data classes module. And
then we're going to define a field. And
we're going to give it a default factory
of a list like so. And now when I run
this again, you see that this works as
expected. Now there's a type error here.
And actually the issue is that this is a
list of strings, but we actually specify
the default factory as just a list. So
if you want to be precise, you also need
to specify the default factory as a list
of strings. Doesn't really change
anything the way that Python works, but
it's more precise in terms of typing.
The second thing you can do with data
class is that it allows for derived
fields. These are fields that shouldn't
be passed into the constructor. You can
mark a field as not being part of the
generated initializer and this lets you
compute it later typically in the post
initialization step and this
particularly useful when the value
should be stable and not recomputed
every time uh unlike for example a
property that recalculates on access.
For example, let's say when we create a
user, we want that use to have a slug
which will point to a URL. Now,
typically this is something you would
create only once per user and you don't
change it if the user information
changes because then the URL doesn't
work anymore. So the slug that's going
to be a field, but we're going to set
that as init false like so. So now this
slug value is not something that you can
set in the user initializer. But what
you can do is define a post init method.
And this is a method that is called
right after the object has been
constructed.
So what you can do here for example is
let's say we have a variable slugified
and that's going to be the name dot
完全な字幕は動画プレーヤーで利用可能
コメント (0)
ログインしてコメントインタラクティブモード
クイズ
正解:
クイズ
正解:
動画を見ながらクイズが表示されます
覚え方のコツ
この動画より
無料で語学を始める