+ 1
what's difference python getter and setter?
4 Respostas
+ 6
Getters allow read-only access, setters can be used to validate the input. Here's an example:
https://code.sololearn.com/crgz6d6tK4LN/?ref=app
0
A getter is used to get the value of a variable inside a class, while setters are used to modify value of that variables.
Exemple :
class Car:
def __init__(self) :
self.x = 0
def getterX(self) :
return self.x
def setterX(self, value) :
self.x = value
0
More specifically what makes the setter?
0
Here, the setter changes the value of x... And that's all!
It's used for encapsulation. We don't change directly the values of the variables from outside a class, but we use setters instead.