Inheriting property functions
Robert Kern
robert.kern at gmail.com
Fri Oct 20 18:57:50 EDT 2006
Dustan wrote:
> Looking at this interactive session:
>
>>>> class A(object):
> def __init__(self, a):
> self.a = a
> def get_a(self): return self.__a
> def set_a(self, new_a): self.__a = new_a
> a = property(get_a, set_a)
>
>
>>>> class B(A):
> b = property(get_a, set_a)
>
>
> Traceback (most recent call last):
> File "<pyshell#11>", line 1, in <module>
> class B(A):
> File "<pyshell#11>", line 2, in B
> b = property(get_a, set_a)
> NameError: name 'get_a' is not defined
>>>> class B(A):
> b = a
>
>
> Traceback (most recent call last):
> File "<pyshell#13>", line 1, in <module>
> class B(A):
> File "<pyshell#13>", line 2, in B
> b = a
> NameError: name 'a' is not defined
>
> B isn't recognizing its inheritence of A's methods get_a and set_a
> during creation.
Inheritance really doesn't work that way. The code in the class suite gets
executed in its own namespace that doesn't know anything about inheritance. The
inheritance rules operate in attribute access on the class object later.
Try this:
class B(A):
b = property(A.get_a, A.set_a)
or this:
class B(A):
b = A.a
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list