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

解锁AI学习工具

注册即可使用强大工具,帮助你从每个视频中更快地学习。

场景解析 短语猎手 词卡复习 跟读练习 语音回放
免费注册
英语 17:05 Science & Tech

Python Properties vs Methods: The Contract You Didn’t Know You Were Making

Late Night with Seth Meyers · 46,891 次观看 · 添加于 1 个月前

字幕 (415 片段)

00:00

Look at these two user account classes.

00:02

Aren't they beautiful? No, they're not

00:05

really beautiful. But as you can see,

00:07

the main difference is that here, this

00:09

one has a method and this user account

00:12

class has a property for roughly the

00:14

same thing. And one of these is a better

00:16

design choice than the other. So today

00:18

I'll talk about when to use a method and

00:21

when to use a property in general and

00:23

give you a few easy guidelines to make

00:25

that decision yourself next time. And

00:29

maybe you have ever wondered whether a

00:31

property can be asynchronous. If you're

00:34

a normal person, you probably haven't

00:36

wondered about it. But if you're like

00:38

weird like me, then maybe you have.

00:41

Watch the video until the end to find

00:42

out because I'm going to talk about that

00:44

as well. Now, if you care about software

00:46

design, not just [clears throat] making

00:47

your code work, but designing code that

00:50

stays clear, predictable, easy to

00:52

change, I'm building something new. It's

00:54

a program called Software Design

00:56

Mastery. It's way more than just another

00:58

online course. It covers literally

01:00

everything I know about software design

01:02

from years of teaching, consulting,

01:04

developing, reviewing real production

01:06

code. If you want to be the first to

01:09

know when it opens, don't want to miss

01:10

early access. Join the wait list.

01:14

The link is also in the video

01:16

description. Now, like I said, we have

01:18

these two user accounts here. There is a

01:20

username and an email address, and

01:22

that's basically it. So why would you

01:25

use a method here versus a property?

01:28

Actually, if you go back to older

01:29

languages like Java, they didn't have

01:32

properties. So you only had getters and

01:34

setters. So you had to do something like

01:37

this always in Java. Now the nice thing

01:39

about properties is that they behave a

01:42

bit like attributes, right? In this

01:44

case, for example, let's see how you

01:46

would use that. So I have my user

01:48

account data class. So if I create a

01:50

user account then I can print the

01:52

username by calling this getter method.

01:55

Pretty straightforward. So uh when I run

01:58

this then as you can see this is what we

02:00

get as a result. Simply prints the

02:02

username. But let's say I'm using user

02:04

account 2 and now of course I don't use

02:07

this getter but I simply access the

02:10

property directly and then this is what

02:13

we get and we have exactly the same

02:15

output. But of course this way of

02:18

accessing the username is way easier

02:21

than using this method. It's it's

02:23

shorter. You don't have to write the

02:24

parenthesis. So it's really easy way to

02:27

access data. And that also informs what

02:31

a property actually communicate. It

02:33

communicates that something is stelike,

02:36

that it's cheap to access, that it's

02:39

safe to read repeatedly. Now method

02:43

typically communicates something else.

02:46

It tells you that probably some work is

02:48

happening. We don't know exactly what

02:51

will happen in a method. A method can be

02:52

something very simple, but it can also

02:54

be extremely complex. It might

02:56

communicate with the back end or a

02:58

database or or something like that. It

03:01

communicates that well something might

03:03

fail. You might get an exception or

03:05

other things might happen. A method

03:07

might be slow. So if you keep these two

03:09

things in mind, it also shows you when

03:11

to typically use a property versus when

完整字幕可在视频播放器中查看

用练习题学习

从这个视频生成词汇、语法和理解练习

词汇与语法 理解测验 雅思考试 写作练习
注册开始练习
还没有评论。成为第一个分享想法的人!

注册解锁全部功能

追踪进度、保存词汇、练习题目

免费开始学语言