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
Desbloquea herramientas de aprendizaje con IA
Regístrate para acceder a herramientas potentes que te ayudan a aprender más rápido con cada video.
Python Properties vs Methods: The Contract You Didn’t Know You Were Making
Subtítulos (415 segmentos)
Look at these two user account classes.
Aren't they beautiful? No, they're not
really beautiful. But as you can see,
the main difference is that here, this
one has a method and this user account
class has a property for roughly the
same thing. And one of these is a better
design choice than the other. So today
I'll talk about when to use a method and
when to use a property in general and
give you a few easy guidelines to make
that decision yourself next time. And
maybe you have ever wondered whether a
property can be asynchronous. If you're
a normal person, you probably haven't
wondered about it. But if you're like
weird like me, then maybe you have.
Watch the video until the end to find
out because I'm going to talk about that
as well. Now, if you care about software
design, not just [clears throat] making
your code work, but designing code that
stays clear, predictable, easy to
change, I'm building something new. It's
a program called Software Design
Mastery. It's way more than just another
online course. It covers literally
everything I know about software design
from years of teaching, consulting,
developing, reviewing real production
code. If you want to be the first to
know when it opens, don't want to miss
early access. Join the wait list.
The link is also in the video
description. Now, like I said, we have
these two user accounts here. There is a
username and an email address, and
that's basically it. So why would you
use a method here versus a property?
Actually, if you go back to older
languages like Java, they didn't have
properties. So you only had getters and
setters. So you had to do something like
this always in Java. Now the nice thing
about properties is that they behave a
bit like attributes, right? In this
case, for example, let's see how you
would use that. So I have my user
account data class. So if I create a
user account then I can print the
username by calling this getter method.
Pretty straightforward. So uh when I run
this then as you can see this is what we
get as a result. Simply prints the
username. But let's say I'm using user
account 2 and now of course I don't use
this getter but I simply access the
property directly and then this is what
we get and we have exactly the same
output. But of course this way of
accessing the username is way easier
than using this method. It's it's
shorter. You don't have to write the
parenthesis. So it's really easy way to
access data. And that also informs what
a property actually communicate. It
communicates that something is stelike,
that it's cheap to access, that it's
safe to read repeatedly. Now method
typically communicates something else.
It tells you that probably some work is
happening. We don't know exactly what
will happen in a method. A method can be
something very simple, but it can also
be extremely complex. It might
communicate with the back end or a
database or or something like that. It
communicates that well something might
fail. You might get an exception or
other things might happen. A method
might be slow. So if you keep these two
things in mind, it also shows you when
to typically use a property versus when
Subtítulos completos disponibles en el reproductor
Practica con ejercicios
Genera ejercicios de vocabulario, gramática y comprensión de este video
Comentarios (0)
Inicia Sesión para ComentarRegístrate para desbloquear todas las funciones
Sigue tu progreso, guarda vocabulario y practica con ejercicios
Modo interactivo
Cuestionario
Respuesta correcta:
Vídeos relacionados
Late Night with Seth Meyers
Cuestionario
Respuesta correcta:
Los quizzes aparecen mientras ves el video
Truco para recordar
De este video
Aprende idiomas gratis