Inheriting property functions

Dustan DustanGroups at gmail.com
Fri Oct 20 18:49:00 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.
>
> 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.

Clarification: The code given is obviously not the actual code. I doubt
it would really make a difference in types A and B given here, but for
what I'm actually doing, it makes a big difference.

> Is there any way to make this work as intended?




More information about the Python-list mailing list