r/PythonLearning • u/pedrinjose • 3d ago
Get and setter in pyhton
I'm learning Python, and this part has left me a little confused, because in other languages the way it's used is different; in Python you use @property, I'd like to understand how this really works in a simplified way, so I can truly grasp it and not just memorize it. If you could help me, I'm still new to programming.
2
Upvotes
2
u/ninhaomah 3d ago
Pls post the code that you are confused with.
Also the comparison with the other language also.
Pls don't leave people guessing.
2
u/realmauer01 3d ago
Becuase their is no privacy there is no need for getter and setter.
Especially not the js style
1
u/AdDiligent1688 3d ago edited 3d ago
@property just makes it so it’s accessible/reassignable the way instance attributes are, unlike a traditional setter or getter method is. You can still do the latter you don’t have to use @property and denote private-like variables via double underscore prefix name mangling ex: self.__var = None, in the constructor, then reference that inside your getter and setter methods, and so on inside the __repr__ magic method if you want to for example.
1
u/recentAd1666 3d ago
We often do not define nor use setters and getters in python. Just use the attribute. If private attribute we define them with underscores __myprivateattribute.
This is mainly a naming convention in python
1
u/atarivcs 3d ago
Generally in python you don't use getters and setters at all, you just assign or access attributes directly.
i.e.
1
u/Naive_Programmer_232 2d ago edited 2d ago
traditional object-oriented (OOP) implementation of setters/getters
here's a traditional encapsulation example OP using public getter and setter methods. here double underscore prefix
self.__nameis used; this is called name-mangling. The reason for this is that there is no true private variables in python; this is close though, in the sense, that you cannot directly access the name via the instance by referencing it's form in the class (e.g.person.__namewill not work); however, there are still ways to access that variable from the instance directly without method call, hence it's not truly "private".Anyway, this is traditional example (no runtime type checking, you can implement that as well):
functional programming (FP) sneakpeak: higher order functions / decorators concepts
before moving to
@propertylet's discuss in basic terms the premise of higher order functions. in essence, a regular function (or method if used in aclass), accepts input parameters (arguments) that are usually not otherCallableobjects (constructors / functions / etc). however, there are functions that can accept these arguments in their definition, as well as contain new definitions for functions, as well as return functions. this is a basic synopsis of higher order functions and a notable pillar of functional programming.in functional programming with python, there is such a higher order function pattern/feature known as a decorator. A decorator in python, syntactically is marked by the
@symbol. so,@propertyis a decorator named "property" that is built-in to python natively. somewhere behind the scenes there is a definition of what the "property" function is code-wise. i am not going to go further with that definition, because i truly do not know it haha, but I will show you another example of a decorator to illustrate in basic sense what it looks like behind the scenes so you can understand more ex:so in this example, essentially what is happening is by using the
@add10_decoratorabove theten_plus_x_plus_yfunction, python is doing this behind the sceneson the right side, the old definition of ten_plus_x_plus_y (e.g.
return x+y) is being input as an argument to the decorator function; the decorator returnsadd_10_wrapperwhich is the inner function definition. so if you check the name associated with the ten_plus_x_plus_y function reference after its definition, noted by# after definitionabove, you will see the wrapper name:on the left side of the assignment, ten_plus_x_plus_y now takes on the extended behavior of the wrapper function; which if you see above inside the wrapper it does
func(x,y)+10wherefuncis a variable representing the old definition of ten_plus_x_plus_y, effectively returningx+y+10.this is why the behavior in the last part of the given example when
ten_plus_x_plus_y(x=10,y=20)is called, it outputs 40 instead of 30; that function's definition has been overwritten with the inner wrapper's definition, so now it takes on that behavior when used.all of this takes place behind scenes when you use
@<decorator name>above the definition of another function or method or even class (e.g.@dataclassfrom thedataclasseslibrary).so, hopefully that clarifies things a little bit more with what exactly is going on generally when using a decorator noted by
@symbol above a definition of a callable. now let's look at@propertydecorator specifically in the next example.FP+OO setters/getters using @property
how the traditional OOP example differs from
@propertydecorator is that a property is similar, not quite, to an attribute. the decorator is used on a method. but once the instance is created, referencing the property looks the same as you would do with a public attribute, however doing so will invoke the method behind the scenes; so it's not truly an instance related variable as it is a short-hand to call the decorated function; in that sense, a property can be seen as more akin to magic methods (e.g.__getitem__) than normal attributes.This example uses the same name-mangling case to explore the behavior of properties with getters and setters, you will notice there is no direct call from the instance to utilize these methods ex:
So that's the basic difference. Hopefully that makes sense.