Newbie: Inheritance of accessors

Henrik Weber Henrik.Weber at
Wed Oct 16 10:35:51 CEST 2002

> I guess I should have been clearer. I meant reading the inherited 
> *default* value from the parent class. Given the same code (repeated 
> below), I see:
>  >>> q = R()
>  >>> print q.x
>  0
>  >>> e = S()
>  >>> print e.x
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 5, in getx
> AttributeError: 'S' object has no attribute '_R__x'
>  >>>
> Can e *not* inherit to the default value? Or is there a way to call the 
> __init__ method for R's superclass (so that 'x' in initialized?).
> WF

The latter. Since your class R is a new style class (inherited from
object), you can write your class S this way:

class S(R):
    def __init__(self):
        super(S, self).__init__()
        self.__y = 0

If you use an old style class (not inherited from object), you would
have had to write:

class S(R):
    def __init__(self):
        self.__y = 0

Details about the super() function and new style classes can be found
in the "What's new" document for Python 2.2.

-- Henrik Weber

More information about the Python-list mailing list