what's difference python getter and setter? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what's difference python getter and setter?

15th May 2019, 2:43 PM
kokito
4 Answers
+ 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
15th May 2019, 5:03 PM
Anna
Anna - avatar
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
15th May 2019, 3:45 PM
Théophile
Théophile - avatar
0
More specifically what makes the setter?
15th May 2019, 3:51 PM
kokito
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.
15th May 2019, 3:53 PM
Théophile
Théophile - avatar