Does Python really follow its philosophy of "Readability counts"?

Steven D'Aprano steve at
Thu Jan 22 14:32:51 CET 2009

On Thu, 22 Jan 2009 10:33:26 +0100, Bruno Desthuilliers wrote:

> Steven D'Aprano a écrit :
>> On Wed, 21 Jan 2009 12:54:31 +0100, Bruno Desthuilliers wrote:
>>> Russ P. a écrit :
>>> (snip)
>>>> In any case, I have suggested that Python should perhaps get a new
>>>> keyword, "private" or "priv".
>>> And quite a few people - most of them using Python daily - answered
>>> they didn't wan't it.
>> Then they don't have to use it.
> Yes they would. Because this would become the official way to tell
> what's interface and what's implementation, and *this* is the important
> point.

But if you have free access to attributes, then *everything* is interface.

>> Lots of people think that double-underscore name mangling is a waste of
>> time: not strict enough to be useful, not open enough to be Pythonic.
>> Solution? Don't use double-underscore names.
> The name-mangling mechanism is actually useful when you want to make
> sure some vital implementation attribute (usually of a class intented to
> be extended by the library users) won't be *accidentally* overwritten.

Except it doesn't. Take this simple module:


class C(object):
    __n = 3
    def spam(self):
        return "spam " * self.__n

class D(C):

# end

I have no interest in C; I may have no idea it even exists. It might be 
buried deep inside the inheritance hierarchy of the class I really want, 
D. So now I subclass D:

>>> from module import D
>>> class C(D):
...     __n = 5
...     def ham(self):
...             return "I eat ham %d times a day" % self.__n
>>> C().ham()
'I eat ham 5 times a day'
>>> assert C().spam() == D().spam()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

And now I have accidentally broken the spam() method, due to a name clash.

Besides, double-underscore names are a PITA to work with:

>>> class Parrot(object):
...     __colour = 'blue'
...     def __str__(self):
...         return 'A %s parrot' % self.__colour
...     __repr__ = __str__
>>> class RedParrot(Parrot): # Just like Parrot, only red.
...     __colour = 'red'
>>> Parrot()
A blue parrot
>>> RedParrot()
A blue parrot


More information about the Python-list mailing list