473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inheriting property functions

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?

Oct 20 '06 #1
9 1336

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?
Oct 20 '06 #2
Dustan wrote:
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:
>>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(A.get_a,A.set_a)
....
>>bar = B(5)
bar.a
5
>>bar.b
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.

Oct 20 '06 #3
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

Oct 20 '06 #4
Robert Kern wrote:
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

Oct 20 '06 #5
At Friday 20/10/2006 19:49, Dustan wrote:
>>class A(object):
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)
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
Oct 20 '06 #6

Robert Kern wrote:
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
Thanks for your help, and mdsteele's.

Oct 21 '06 #7
In article <11*********************@m73g2000cwd.googlegroups. com>,
Dustan <Du**********@gmail.comwrote:
>
>>>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)
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
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it." --Dijkstra
Oct 21 '06 #8
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
Oct 21 '06 #9
md******@gmail.com 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:
>>class A(object):
.... aa = "aa"
....
>>class B(A):pass
....
>>B.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.

Oct 21 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Winter | last post by:
This is in IE 6... I'm trying to inherit from built-in JS objects - Array, specifically. However, I'm having some difficulties. I can add array elements to the child object, and when I retrieve...
1
by: Yasutaka Ito | last post by:
Hi, I have a base form (FormBase), which defines things like the value of the WindowState. Other forms inherit from this base form to reflect the layout given in the base form. Is there a way...
15
by: JustSomeGuy | last post by:
this doesn't want to compile.... class image : public std::list<element> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element...
29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
3
by: Roy Soltoff | last post by:
Two books, "Mastering Visual Basic.Net" and "Visual Basic.Net Developer's Handbook" describe inheriting from System.EventArgs using a class similar to: Public Class MyEventArgs Inherits...
3
by: Alex Satrapa | last post by:
There's some mention in the (old!) documentation that constraints such as foreign keys won't include data from inheriting tables, eg: CREATE TABLE foo ( id SERIAL PRIMARY KEY ); CREATE TABLE...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
5
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.