Python Setter and Getter Naming

Related searches

I'm trying to understand how getters and setters work in python with properties, but I'm confused in the naming convention.

Is there a specific naming standard for the getter and setter? Why do I need to set the returning name of parameter with an "_" (underscore)? If I were to change it to just "self.color" instead of "self._color", the getter and setter no longer work. Also, if I were to change the name of the functions the print function is no longer is executed. Any clarification would be greatly appreciated, thanks!

class FavoriteColor:
    def __init__(self, color):
        self.color = color

    @property
    def color(self):
        print('getter method')
        return self._color

    @color.setter
    def color(self, x):
        print('setter method')
        self._color = x

obj1 = FavoriteColor('blue')


If you did this:

@color.setter
def color(self, x):
    self.color = x

Then this setter would call itself repeatedly. There's no difference between obj1.color = 'blue' and self.color = x, they both do the same thing, which is to invoke the setter def color. So with the setter invoking itself recursively, you have an endless recursive loop which will eventually crash your program.

For that purpose you need to actually store the value on some other attribute. Using "_color" for its name is just the rather obvious solution.

Note that using this kind of setter/getter in Python is frowned upon, if your setter/getter doesn't do anything. You can remove them entirely and just plainly set self.color = 'blue' for exactly the same effect as your setter/getter pair currently has. You should only use setters/getters if they do some additional processing. Since the syntax of a plain attribute and a setter/getter are identical, you can even safely transition to using setters/getters later on if you need to (in contrast to, say, Java, where a setter/getter is foo.setBar(...), which is not identical to foo.bar = ... and cannot be transparently substituted later).

Python Tutorial: Properties vs. getters and setters, Recommended Posts: R vs Python � Sockets | Python � Python issubclass() � Python: Map VS For Loop � Python Variables � Name mangling in� Using "_color" for its name is just the rather obvious solution. Note that using this kind of setter/getter in Python is frowned upon, if your setter/getter doesn't do anything. You can remove them entirely and just plainly set self.color = 'blue' for exactly the same effect as your setter/getter pair currently has. You should only use setters


I guess the confusing part is in the __init__. The self.color = color in __init__ is actually calling your setter, which creates the private variable called _color. The setter and getter work as wrappers of this private _color allowing you to do something extra than accessing the raw variable directly.

I don't know if this is a common practice to call setter in __init__, but I prefer to defining the wrapped private variable in __init__ directly. To me it looks more straightforward

class FavoriteColor:
    def __init__(self, color):
        self._color = color

Getter and Setter in Python, If you did this: @color.setter def color(self, x): self.color = x. Then this setter would call itself repeatedly. There's no difference between obj1.color� Unfortunately, it is widespread belief that a proper Python class should encapsulate private attributes by using getters and setters. As soon as one of these programmers introduces a new attribute, he or she will make it a private variable and creates "automatically" a getter and a setter for this attributes.


Like most of the programming languages, Python has convention to define private variables of a class with an underscore "_", so by naming "_color" we are meant to create a private attribute 'color'.

And for your second question, you are meant to define getter setter property for the attribute that you just defined, According to rules of property decorator, we must have to define same name of getter setter methods as that of our targeted attribute along with @property decorator.

Hope it helps

class FavoriteColor:
     def __init__(self, color): 
          self._color = color

     # a getter funcion
     @property
     def color(self): 
         return self._color 

     # a setter function 
     @color.setter 
     def color(self, value): 
        self._color = value

Python Setter and Getter Naming, will learn what the difference is between Python Property and Getters & Setters. You can access it directly using the name of the attributes. Using getters and setters. In the below examples we will make a class, initialize is and then add a getter and setter method to each of them. Then access the variables in these methods by instantiating the class and using these getter and setter methods. So you can hide your logic inside the setter method. Example. Live Demo


Python variables and functions share the same namespace. Thus self.color can only refer to either the getter/setter method, or the attribute itself. A common convention is to add an underscore to the internal attribute name, to avoid this naming clash, which also conventionally conveys the notion that it is a private attribute.

This should already explain your other question. If you have a method named self.color and it contains code where it replaces itself with a value, then of course you can no longer use self.color to refer to the method again.

Let's look at that again with an even simpler example.

>>> class X:
...   def color (self, value):
...     self.color = value

The first time you call the function - say, xinstance.color("red") - it will set the instance's color to "red". So then you can no longer call xinstance.color() again because it is now a string, not a function.

Adding the @property decorator changes this somewhat, because now self.color = value will cause color to call itself, which causes it to call itself, which causes it to call itself again, etc.

Property vs. Getters and Setters in Python, As the name suggests, getters are the methods which help access the private attributes or get the value of the private attributes and setters are the methods which help change or set the value of private attributes. Here is the way to implement getters and setters in Python. class CurrencyConverter: def __init__(self, currency_x = 1): self.set_currency_x(currency_x) def currency_converter(self): currency_y = self.get_currency_x() * 28 return (currency_y) #implementing new getter method def get_currency_x(self): return (self.currency_x) #implementing new setter method def set_currency_x(self, currency


Getter and Setter in Python, Python programming provides us with a built-in @property decorator which makes usage of getter and setters much easier in Object-Oriented Programming.. Before going into details on what @property decorator is, let us first build an intuition on why it would be needed in the first place.


Properties in favor of getters/setters. Getters and setters that are simple, e.g. no parameters in the getter and a single in the setter, should be a property. However, if there are significant side effects to the getter or the setter, that must be made clear to the programmer, use the function style. Do not use get_ Prefer to use the name


Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case: Example public class Person { private String name; // private = restricted access // Getter public String getName() { return name; } // Setter public void setName(String newName) { this.name = newName; } }