If in doubt, leave this "public" - I mean, do not add anything to hide the name of your attribute. If you have a class with some internal value, don't worry about that. Instead of writing:
class Stack(object): def __init__(self): self.__storage = []
write this by default:
class Stack(object): def __init__(self): self.storage = []
This is probably a controversial way of doing things. Python newbies just hate it, and even some old Python guys despise this default value, but it's the default anyway, so I highly recommend you follow it even if you feel awkward.
If you really want to send the message "Can't touch this!" for your users, the usual way is to prefix the variable with one underscore. This is just an agreement, but people understand it and take double care when dealing with such things:
class Stack(object): def __init__(self): self._storage = []
It can also be useful to avoid a conflict between property names and attribute names:
class Person(object): def __init__(self, name, age): self.name = name self._age = age if age >= 0 else 0 @property def age(self): return self._age @age.setter def age(self, age): if age >= 0: self._age = age else: self._age = 0
What about double underlining? So, double underscore magic is mainly used to avoid accidental overloading of methods and name conflicts with attributes of superclasses . This can be very useful if you write a class that is expected to be extended multiple times.
If you want to use it for other purposes, you can, but this is neither ordinary nor recommended.
EDIT : Why is this so? Well, the usual Python style does not emphasize the need to make things private - on the contrary! There are many reasons for this - most of them are contradictory ... Let's look at some of them.
Python has properties
Today, most OO languages use the opposite approach: what should not be used should not be visible, so attributes should be private. Theoretically, this would lead to more manageable, less connected classes, because no one could recklessly change values inside objects.
However, this is not so simple. For example, Java classes have many attributes and retrieval methods that simply get values and setter methods that just set values. You need, say, seven lines of code to declare a single attribute - which, as the Python programmer would say, is unnecessarily complicated. In addition, in practice, you simply write all this code to get one open field, since you can change its value using the get and set methods.
So why follow this default policy? Just make your attributes public by default. Of course, this is problematic in Java, because if you decide to add some validation to your attribute, you will need to change everything
person.age = age;
in your code let's say
person.setAge(age);
setAge() creature:
public void setAge(int age) { if (age >= 0) { this.age = age; } else { this.age = 0; } }
Thus, in Java (and other languages), getters and setters are still used by default, because they can be annoying, but can save a lot of time if you find yourself in the situation I described.
However, you do not need to do this in Python, as Python has properties. If you have this class:
class Person(object): def __init__(self, name, age): self.name = name self.age = age
and then you decide to confirm the age, you do not need to change parts of the person.age = age code. Just add a property (as shown below)
class Person(object): def __init__(self, name, age): self.name = name self._age = age if age >= 0 else 0 @property def age(self): return self._age @age.setter def age(self, age): if age >= 0: self._age = age else: self._age = 0
If you can do this and still use person.age = age , why add personal fields, retrieval and installation methods?
(Also see Python not Java and this article about the dangers of using getters and setters .).
In any case, everything is visible - and trying to hide only complicates your work.
Even in languages where there are personal attributes, you can access them through some kind of reflection / introspection library. And people do this a lot, within the framework and for solving urgent needs. The problem is that introspection libraries are just a tricky way to do what you could do with public attributes.
Since Python is a very dynamic language, adding this burden to your classes is simply counterproductive.
The problem is impossible to see - it is needed to see
For Pythonista, encapsulation is not an inability to see the internals of classes, but the ability to avoid viewing them. I mean that encapsulation is a property of a component that allows you to use it without worrying about the internal details of the user. If you can use a component without worrying about its implementation, then it is encapsulated (according to the Python programmer).
Now, if you have written your class this way, you can use it without thinking about the implementation details, there will be no problems if you want to look inside the class for some reason. The point is, your API should be good, and everything else should be the details.
Guido said so
Well, this is not controversial: he said so, really . (Look for the "open kimono.")
It's a culture
Yes, there are several reasons, but not critical. This is basically the cultural aspect of Python programming. Honestly, this may be another way, but it is not. In addition, you could just as easily ask another question: why do some languages use private attributes by default? For the same main reason as for Python practice: because it is the culture of these languages, and each choice has its advantages and disadvantages.
Since this culture already exists, you are advised to follow it. Otherwise, you will be annoyed by Python programmers who will tell you to remove __ from your code when you ask a question in Stack Overflow :)