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.
Python Properties vs Methods: The Contract You Didn’t Know You Were Making
Legendas (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
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
Comentários (0)
Faça Login para ComentarCadastre-se para desbloquear todos os recursos
Acompanhe seu progresso, salve vocabulário e pratique com exercícios
Modo interativo
Quiz
Resposta certa:
Vídeos relacionados
Now in Android: 120 – Android Canaries, Agentic AI, 16 KB page size requirements, and more!
Two-Minute Case Study - Artificial Intelligence: Sephora's Newest Beauty Advisor
Two-Minute Case Study - Data & Gabbana: Stitch Fix's Predictive Algorithms
Top 3 Google Play Google I/O 2025 announcements
I'm Really Starting To Like Coding With AI
Late Night with Seth Meyers
Quiz
Resposta certa:
Os quizzes aparecem enquanto você assiste ao vídeo
Dica de memorização
Deste vídeo
Aprenda idiomas de grátis