OOP techniques in Python
Steven Bethard
steven.bethard at gmail.com
Fri Apr 28 12:12:45 EDT 2006
Dennis Lee Bieber wrote:
> On Thu, 27 Apr 2006 14:32:15 -0500, Philippe Martin
> <pmartin at snakecard.com> declaimed the following in comp.lang.python:
>
>> What then is the point of the double underscore (if any) ?:
>
> To prevent masking/shadowing of inherited attributes...
Note that it can fail to do this if you accidentally (or purposefully)
name a class the same as a parent or ancestor class:
>>> class Confusion(object):
... def __init__(self):
... self.__str = 'module 1'
... def get_module_1_confusion(self):
... return self.__str
...
>>> module_1_confusion = Confusion
>>> class Confusion(module_1_confusion):
... def __init__(self):
... self.__str = 'module 2'
...
>>> module_2_confusion = Confusion
>>> module_2_confusion().get_module_1_confusion()
'module 2'
So you still need to at least check that the name of your subclass is
different from the name of all its ancestors. This doesn't come up that
often, but I know a few people have been bitten by it.
STeVe
More information about the Python-list
mailing list