THIS Is How You Should Be Making Getters & Setters In Python

preview_player
Показать описание
This is how you should be making getters & setters in Python. It's simple and doesn't require any change to existing variable names. In the example I changed "_name" to "fruit_name", but you don't have to do that, you can easily just call it "name".

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

This was the most easy tutorial for me to understand this super confusing thing. Thanks!

ordinaryBrain
Автор

Big Man Ting, this is actually great stuff and simple

ganzaian
Автор

Awesome dude. I finally understood it. 🎉

LEGENDKINETIC
Автор

Thanks, you help me much for work and studies :)

lisav
Автор

Thanks, this will allow checking values before writing them

DerBarde
Автор

The only circumstance that I think can make the setter and getter useful is to add additional functionality like checking the validity of the input. Yet, I do not know whether this is a good practice :/

Shao-LunHuang
Автор

thank you so much i was so confused before i saw your video

lovemesy
Автор

there's as 400 long page articles and this pro achieved to explain it into seven minutes

the_wizard_exe
Автор

What is the advantage of this property wrappers compared to use the class variable directly?
At the beginning you talk about private variables. But the variable "_name" is still not private with these property wrappers, right? Currently, it feels like #pragma in C to map one variable to another and I don't really see the advantage. It adds way more code compared to use the class variable directly. Am I missing something?

iHaAcKs
Автор

Very good, but, can you enlarge the screen?😊

michel
Автор

Can't we just use "__get__" and "__set__" dunder method for implementing getters and setters in a class. Can someone explain ?

jasmeet
Автор

I thought declaring an attribute with an underscore makes it protected rather than private.

_KobbyOb
Автор

Honestly that just seems longer and less readable to me, but if there's a good reason to use it I'm happy to be proven wrong!

dalkap
Автор

This is longer.... Why would I do that

aventurileluipetre
Автор

People he made this tutorial because it's a valid Python thing and its confusing. This is for those who wants to understand it. For example, I'm just following a curriculum that has this decorator in it. So this was useful to me. Stop hating.

ordinaryBrain
Автор

namin' attribute startin' with 2 _ will make it private it is my observation

mihaiciorobitca
Автор

Seems like this method is old school and using it kind of downplays on the advantages of python. Regardless everyone says it's a good concept so I'll practice it I suppose?

Etk
Автор

Why not declare fruit name as private using the double underscore trick?

class Fruit:
def __init__(self, name):
self.__name = name

@property
def name(self):
return self.__name

@name.setter
def name(self, new_name):
if type(new_name) == str:
self.__name = new_name
else:
print("New name must be a string!")

fruit = Fruit("banana")

print(fruit.name)
fruit.name = 45 # here an error occurs
fruit.name = "orange"
print(fruit.name)

OUTPUT:
banana
New name must be a string!
orange

Davidozz
Автор

__name #private
_name #protect
name #public

techiehkr
Автор

This is really confusing i mean why we need a setter when we can access a private property outside a class I mean simply fruit.fruit_name = '''orange ' will change the name what is the point of setters and getters

evilservo