Inheriting property functions

D

Dustan

Looking at this interactive session:
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)

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 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?
 
D

Dustan

Dustan said:
Looking at this interactive session:

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)


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
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.
 
M

mdsteele

Dustan said:
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?

Try this:
.... 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)
........ b = property(A.get_a,A.set_a)
....5

The trouble is that get_a and set_a are attributes of the _class
object_ A. Instances of A (and hence, instances of B) will see them,
but the class B will not, so you have to point to them explicitly with
A.get_a and A.set_a.
 
R

Robert Kern

Dustan said:
Looking at this interactive session:

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)


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
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
 
M

mdsteele

Robert said:
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.

Right. That was what I should have said, but it came out wrong when I
tried to say it. (-:

-Matt
 
G

Gabriel Genellina

Use instead:

b = property(A.get_a, A.set_a)


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
D

Dustan

Robert said:
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

Thanks for your help, and mdsteele's.
 
A

Aahz

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)


b = property(get_a, set_a)

BTW, since you're almost certainly going to run into this quickly given
the direction your code is taking (and also to fix some bugs):

class A(object):
def __init__(self, a):
self._a = a

def get_a(self):
return self._a

def _get_a(self):
return self.get_a()

def set_a(self, new_a):
self._a = new_a

def _set_a(self, new_a):
self.set_a(new_a)

a = property(_get_a, _set_a)

class B(A):
def get_a(self):
return str(self._a)

Thank Alex Martelli for this demonstration that programming is all built
on one basic trick: add another layer of indirection. However, I leave
you to figure out on your own why this is better.

Note carefully that I changed __a to _a. You almost never want to use
double-underscore private names because of the way they cause problems
with inheritance.

PS: Please do NOT post code with TABs
 
D

Diez B. Roggisch

Dustan said:
Looking at this interactive session:

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)


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
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
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
(snip)
The trouble is that get_a and set_a are attributes of the _class
object_ A. Instances of A (and hence, instances of B) will see them,
but the class B will not,

Yes it does:
.... aa = "aa"
....
so you have to point to them explicitly with
A.get_a and A.set_a.

Actually, while the solution is good, the explanation is wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top