OOP techniques in Python

Philippe Martin pmartin at snakecard.com
Mon May 1 08:32:51 EDT 2006


Thanks,

Did not know that.

Philippe



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...
> 
>>>> class A(object):
> ...   def __init__(self):
> ...           self.__WhoMe = "From A"
> ...           print "A : ", dir(self)
> ...           super(A, self).__init__()
> ...
>>>> class B(object):
> ...   def __init__(self):
> ...           self.__WhoMe = 42
> ...           print "B : ", dir(self)
> ...           super(B, self).__init__()
> ...
>>>> class Confusion(A, B):
> ...   def __init__(self):
> ...           self.__WhoMe = "I'm confuzzled"
> ...           print "Confusion: ", dir(self)
> ...           super(Confusion, self).__init__()
> ...
>>>> cab = Confusion()
> Confusion:  ['_Confusion__WhoMe', '__class__', '__delattr__',
> '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__',
> '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__str__', '__weakref__']
> A :  ['_A__WhoMe', '_Confusion__WhoMe', '__class__', '__delattr__',
> '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__',
> '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__str__', '__weakref__']
> B :  ['_A__WhoMe', '_B__WhoMe', '_Confusion__WhoMe', '__class__',
> '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__',
> '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
> '__repr__', '__setattr__', '__str__', '__weakref__']
>>>> 
> 
> Note that A, B, and Confusion each have "__WhoMe". Also notice how
> each __init__ invokes the parent module __init__; each one adds its
> __WhoMe to the object without masking those defined in others.
> 
> Without the __, you'd have only ONE attribute after all of that; as
> shown next...
> 
>>>> class A(object):
> ...   def __init__(self):
> ...           self._WhoMe = "From A"
> ...           print "A : ", dir(self)
> ...           super(A, self).__init__()
> ...
>>>> class B(object):
> ...   def __init__(self):
> ...           self._WhoMe = 42
> ...           print "B : ", dir(self)
> ...           super(B, self).__init__()
> ...
>>>> class Confusion(A, B):
> ...   def __init__(self):
> ...           self._WhoMe = "I'm confuzzled"
> ...           print "Confusion: ", dir(self)
> ...           super(Confusion, self).__init__()
> ...
>>>> cab2 = Confusion()
> Confusion:  ['_WhoMe', '__class__', '__delattr__', '__dict__',
> '__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
> '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
> '__str__', '__weakref__']
> A :  ['_WhoMe', '__class__', '__delattr__', '__dict__', '__doc__',
> '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> '__weakref__']
> B :  ['_WhoMe', '__class__', '__delattr__', '__dict__', '__doc__',
> '__getattribute__', '__hash__', '__init__', '__module__', '__new__',
> '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
> '__weakref__']
>>>> 
> --
>  > ============================================================== <
>  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
>  >      wulfraed at dm.net     |       Bestiaria Support Staff       <
>  > ============================================================== <
>  >           Home Page: <http://www.dm.net/~wulfraed/>            <
>  >        Overflow Page: <http://wlfraed.home.netcom.com/>        <




More information about the Python-list mailing list