Inheriting property functions

Dustan DustanGroups at gmail.com
Sat Oct 21 00:28:06 CEST 2006


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.

Is there any way to make this work as intended?




More information about the Python-list mailing list