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

Desbloqueie ferramentas de aprendizado com IA

Cadastre-se para acessar ferramentas poderosas que ajudam a aprender mais rápido com cada vídeo.

Explicador de cena Caça-frases Revisão com flashcards Prática de repetição Falar de Volta
Cadastrar grátis
Inglês 20:24 Science & Tech

7 Things You Didn’t Know Dataclasses Could Do

Late Night with Seth Meyers · 37,849 visualizações · Adicionado há 1 mês

Legendas (469 segmentos)

00:00

Most developers know the basics of data

00:02

classes in Python. You define a simple

00:05

class with some typed fields and then

00:07

Python generates the boiler plate. Here

00:09

you see an example of that. I have a

00:11

user class. It's a data class. A user

00:14

has a name and an email address. And

00:16

because it's a data class, I can now

00:17

initialize it as following. And I can

00:19

simply print it. And it's going to print

00:21

out the information in a readable way.

00:24

Here you can see the result of running

00:26

this script. But data classes can do way

00:29

more than that. So today I'll show you

00:31

seven things you probably don't know

00:33

about data classes. Each of which makes

00:36

your code safer, clearer, and easier to

00:38

maintain. And the last one tends to

00:41

surprise people. If you want to learn

00:43

how to design software from scratch,

00:45

grab my free guide at iron.code/design

00:47

guide. It walks you through my sevenstep

00:49

process for designing new software. Link

00:52

is in the video description. The first

00:54

thing that's really easy to do with data

00:56

classes is default values. For example,

00:58

if I have an active value that's

01:00

boolean, I can by default make this true

01:03

by simply assigning it a value. Then of

01:06

course, if I run this now, each of these

01:08

users are going to be active by default.

01:11

However, since it's a default value, I

01:14

can still override this and make one of

01:17

these users false like so. So then we

01:20

get this as a result. Now defaults are

01:23

sometimes a bit complicated in

01:25

particular if you're dealing with more

01:27

complicated types. Let's say you have a

01:30

tag which is a list of strings. Last a

01:34

list of strings. Now you might think you

01:38

can do this. However, this is

01:39

problematic because actually this is an

01:42

empty list and that's generated only

01:44

once when Python runs the script. And if

01:47

I run that now, you see actually data

01:49

class protects us against this by

01:51

raising a value error. So instead of

01:54

doing this, what you need to do is

01:56

define a field which you also need to

01:59

import from the data classes module. And

02:03

then we're going to define a field. And

02:05

we're going to give it a default factory

02:08

of a list like so. And now when I run

02:11

this again, you see that this works as

02:13

expected. Now there's a type error here.

02:15

And actually the issue is that this is a

02:17

list of strings, but we actually specify

02:19

the default factory as just a list. So

02:22

if you want to be precise, you also need

02:24

to specify the default factory as a list

02:26

of strings. Doesn't really change

02:28

anything the way that Python works, but

02:30

it's more precise in terms of typing.

02:33

The second thing you can do with data

02:34

class is that it allows for derived

02:37

fields. These are fields that shouldn't

02:39

be passed into the constructor. You can

02:41

mark a field as not being part of the

02:44

generated initializer and this lets you

02:46

compute it later typically in the post

02:49

initialization step and this

02:51

particularly useful when the value

02:53

should be stable and not recomputed

02:55

every time uh unlike for example a

02:57

property that recalculates on access.

03:00

For example, let's say when we create a

03:02

user, we want that use to have a slug

03:05

which will point to a URL. Now,

03:07

typically this is something you would

03:08

create only once per user and you don't

03:11

change it if the user information

03:13

changes because then the URL doesn't

03:15

work anymore. So the slug that's going

03:17

to be a field, but we're going to set

03:19

that as init false like so. So now this

03:23

slug value is not something that you can

03:25

set in the user initializer. But what

03:27

you can do is define a post init method.

03:33

And this is a method that is called

03:34

right after the object has been

03:36

constructed.

03:38

So what you can do here for example is

03:41

let's say we have a variable slugified

03:45

and that's going to be the name dot

Legendas completas disponíveis no player de vídeo

Pratique com exercícios

Gere exercícios de vocabulário, gramática e compreensão deste vídeo

Vocabulário e gramática Quiz de compreensão Exame IELTS Prática de escrita
Cadastre-se pra praticar
Nenhum comentário ainda. Seja o primeiro a compartilhar suas ideias!

Cadastre-se para desbloquear todos os recursos

Acompanhe seu progresso, salve vocabulário e pratique com exercícios

Aprenda idiomas de grátis