473,395 Members | 1,629 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.

In an inherited class, "embedded" classes is referenced?

Hello

I stumpled upon this "feature" during my work tonight, and found it
a bit confusing:
>>class A(object):
.... class C:
.... foobar = 42
....
>>class B(A): pass
....
>>A.C
<class __main__.C at 0xb7cf735c>
>>B.C
<class __main__.C at 0xb7cf735c>
>>B.C.foobar = 60
A.C.foobar
60

When I inherit B from A, I would expect that A.C and B.C would be two
different classes? But apparently not.

Can anyone give me an explanation? Or a better workaround than
something along the line of:
>>B.C = type("C", (object,), {'foobar': 60})
Instead of:
>>B.C.foobar = 60
Thanks,

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Dec 19 '07 #1
4 1453
On Dec 19, 4:23 pm, Christian Joergensen <m...@razor.dkwrote:
Hello

I stumpled upon this "feature" during my work tonight, and found it
a bit confusing:
>class A(object):

... class C:
... foobar = 42
...>>class B(A): pass
...
>A.C

<class __main__.C at 0xb7cf735c>>>B.C

<class __main__.C at 0xb7cf735c>>>B.C.foobar = 60
>A.C.foobar

60

When I inherit B from A, I would expect that A.C and B.C would be two
different classes? But apparently not.
No, when a class inherits a class member from a subclass, both classes
reference the same object. This is true of any object: classes,
lists, sets, etc. For instance, if you were to do this,

class A(object):
class C(object): pass
d = [1,2,3,4]
e = set(("a","b","c","d"))

class B(A):
pass
Then you would find that

A.C is B.C
A.d is B.d
A.e is B.e

They are all the same object.

Perhaps you are misled by the example methods? Even them, the same
function object is referenced by both classes. The difference is,
when accessing a method, a class doesn't return the function object
itself, but a method object instead (which is a binding between a
function and a class, used to set the value of "self" when calling
it).

But only functions do something like that, not classes.

Can anyone give me an explanation? Or a better workaround than
something along the line of:
>B.C = type("C", (object,), {'foobar': 60})
Well you could do this and not bother with the type call (but you'd
still have to do it by hand).

class B(A):
class C(A.C):
foobar = 60
Metaclass programming, or at least some clever properties, would be
required to do it automatically. You could try something like this
(untested) to automatically subclass any class variables that are
instances of type:
class AutoSubclassMetaclass(type):
def __new__(cls,name,bases,clsdict):
for key,value in clsdict.iteritems():
if isinstance(value,type):
clsdict[key] = type(value.__name__,(value,),{})
type.__new__(cls,name,bases,clsdict)
class A(object):
__metaclasS__ = AutoSubclassMetaclass
class C(object):
foobar = 40

class B(A):
pass
Carl Banks
Dec 19 '07 #2
Christian Joergensen schrieb:
Hello

I stumpled upon this "feature" during my work tonight, and found it
a bit confusing:
>>>class A(object):
... class C:
... foobar = 42
...
>>>class B(A): pass
...
>>>A.C
<class __main__.C at 0xb7cf735c>
>>>B.C
<class __main__.C at 0xb7cf735c>
>>>B.C.foobar = 60
A.C.foobar
60

When I inherit B from A, I would expect that A.C and B.C would be two
different classes? But apparently not.

Can anyone give me an explanation? Or a better workaround than
something along the line of:
Why should they be different? The class-statment of A is only exectuted
once, as is the nested class' C. Which makes A.C just a "normal"
class-variable. That is of course shared amongst subclasses. As are
methods, properties and every other thing living in the A.__dict__ due
to the MRO in python.

The more important question is: what do you need C for? Do you have by
any chance a Java-background and think of C as inner/nested class as in
Java? This feature doesn't exist in Python.

Your workaround might be implementable using a metaclass in a more
conveinient way, but I'm not sure-footed enough with metaclasses to
provide a solution out of my head now.

Diez
Dec 19 '07 #3
Carl Banks <pa************@gmail.comwrites:

[...]
No, when a class inherits a class member from a subclass, both classes
reference the same object. This is true of any object: classes,
lists, sets, etc. For instance, if you were to do this,

class A(object):
class C(object): pass
d = [1,2,3,4]
e = set(("a","b","c","d"))

class B(A):
pass
Then you would find that

A.C is B.C
A.d is B.d
A.e is B.e

They are all the same object.
I see.
Perhaps you are misled by the example methods? Even them, the same
function object is referenced by both classes. The difference is,
when accessing a method, a class doesn't return the function object
itself, but a method object instead (which is a binding between a
function and a class, used to set the value of "self" when calling
it).

But only functions do something like that, not classes.
Great explanation. This makes sense. I didn't think of it that way.

[...]
Metaclass programming, or at least some clever properties, would be
required to do it automatically. You could try something like this
(untested) to automatically subclass any class variables that are
instances of type:
class AutoSubclassMetaclass(type):
def __new__(cls,name,bases,clsdict):
for key,value in clsdict.iteritems():
if isinstance(value,type):
clsdict[key] = type(value.__name__,(value,),{})
type.__new__(cls,name,bases,clsdict)
class A(object):
__metaclasS__ = AutoSubclassMetaclass
class C(object):
foobar = 40

class B(A):
pass
Intriguing :-)

Thank you for your timed,

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Dec 21 '07 #4
"Diez B. Roggisch" <de***@nospam.web.dewrites:

[...]
The more important question is: what do you need C for? Do you have by
any chance a Java-background and think of C as inner/nested class as
in Java? This feature doesn't exist in Python.
I was working on some framework code where each class would have a
nested class containing meta information about the outer class
(because all properties would be treated in a special way by the
framework).

I needed to inherit two classes and got confused when they didn't
inherit their meta information - but referenced it :)
Your workaround might be implementable using a metaclass in a more
conveinient way, but I'm not sure-footed enough with metaclasses to
provide a solution out of my head now.
That would be fun ;-)

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Dec 21 '07 #5

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

Similar topics

1
by: nobody | last post by:
hi there! given <!ELEMENT a (#PCDATA | x)*> <!ELEMENT x (#PCDATA)> how can I find out if x is "embedded" at the beginning <a><x>xxx</x>aaa</a> or at the end <a>aaa<x>xxx</x></a> or in the...
3
by: Edward Diener | last post by:
I am creating a component and I want one of my properties to be an embedded class with its own properties. When the component designer shows this property I want it to be able to expand this...
2
by: Markus Dehmann | last post by:
I'd like to process text or document templates that use "embedded C++". Here is a constructed example (the texts I have in mind are much longer and contain relatively few code blocks): ...
5
by: Xiangliang Meng | last post by:
Hi, all. What are the benefit and the drawback of defining a class embedded inside another class? For example: class List { public:
59
by: Jeff Bowden | last post by:
For ease of configuration and other reasons, I would like for my single-user GUI app to be able to use postgresql in-process as a library accessing a database created in the users home directory. ...
3
by: jerome.pouiller | last post by:
I'd like to do this: class foo: public bar::mho { public: class mho { }; }; class bar: public foo::mho { public: class mho { };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.