[Tutor] inheriting private variables...

Alan Gauld alan.gauld@blueyonder.co.uk
Wed Jun 4 09:46:02 2003


> and call the inherited method get_prv_var... like so...
>
> print x.get_prv_var()
>
> this will result to an error message... saying that _a__prv_var 
> is not defined...

It works OK for me:

>>> class A:
...    def __init__(s): s.__priv = 42
...    def getPriv(s): return s.__priv
...
>>> class B(A): pass
...
>>> b = B()
>>> b
<__main__.B instance at 0xa0d1d60>
>>> dir(b)
['_A__priv', '__doc__', '__init__', '__module__', 'getPriv']
>>> b.__priv
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: B instance has no attribute '__priv'
>>> b.getPriv()
42
>>>

Alan G