pythonooppropertiesattributesencapsulation

Should I use "public" attributes or "public" properties in Python?


In Python, I have the following example class:

class Foo:
    self._attr = 0

    @property
    def attr(self):
        return self._attr

    @attr.setter
    def attr(self, value):
        self._attr = value

    @attr.deleter
    def attr(self):
        del self._attr

As you can see, I have a simple "private" attribute "_attr" and a property to access it. There is a lot of codes to declare a simple private attribute and I think that it's not respecting the "KISS" philosophy to declare all attributes like that.

So, why not declare all my attributes as public attributes if I don't need a particular getter/setter/deleter?

My answer will be: Because the principle of encapsulation (OOP) says otherwise!

What is the best way?


Solution

  • Typically, Python code strives to adhere to the Uniform Access Principle. Specifically, the accepted approach is:

    The main advantage to this approach is that the caller gets to do this:

    foo.x += 1
    

    even though the code might really be doing:

    foo.set_x(foo.get_x() + 1)
    

    The first statement is infinitely more readable. Yet, with properties, you can add (at the beginning, or later on) the access control you get with the second approach.

    Note, too, that instance variables starting with a single underscore are conventionally private. That is, the underscore signals to other developers that you consider the value to be private, and they shouldn't mess with it directly; however, nothing in the language prevents them from messing with it directly.

    If you use a double leading underscore (e.g., __x), Python does a little obfuscation of the name. The variable is still accessible from outside the class, via its obfuscated name, however. It's not truly private. It's just kind of ... more opaque. And there are valid arguments against using the double underscore; for one thing, it can make debugging more difficult.