In an inherited class, "embedded" classes is referenced?
Christian Joergensen
mail at razor.dk
Fri Dec 21 06:10:40 EST 2007
Carl Banks <pavlovevidence at gmail.com> writes:
[...]
> 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
More information about the Python-list
mailing list