Inheriting property functions
Diez B. Roggisch
deets at nospam.web.de
Sat Oct 21 03:32:44 EDT 2006
Dustan schrieb:
> 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.
>
> Why am I doing this? For an object of type B, it makes more sense to
> reference the attribute 'b' than it does to reference the attribute
> 'a', even though they are the same, in terms of readability.
I think you are having a code smell here. However, this is how you do it:
class B(A):
b = A.a
Diez
More information about the Python-list
mailing list