Question about accessing class-attributes.

Bjorn Pettersen BPettersen at NAREX.com
Thu Apr 24 17:44:51 CEST 2003


> From: Duncan Booth [mailto:duncan at NOSPAMrcp.co.uk] 
> 
> Alex Martelli <aleax at aleax.it> wrote in 
> news:EARpa.120$3M4.2728 at news1.tin.it:
> 
> > ..isn't this smoother and handier?  Or does it just look that way
> > to me, due to overexposure to custom metaclasses?-)
> > 
> Yes, I agree that it is better, but I have one niggling worry 
> about this:
> 
> Your metaclass works fine for the one requirement given here, 
> and other metaclasses you have proposed work fine in their 
> place, but > what happens if you want to combine these and 
> get e.g. a Bunch which counts its instances?

Well, for some reason Python requires that if C1.__metaclass__ == M1,
C2.__metaclass__ == M2, and C2 is a subclass of C1, then M2 must be a
subclass of all metaclasses of of C2.__bases__, in particular M1. I
haven't seen a rationale for this (and it intuitively seems like an
arbitrary restriction... E.g. given two objects a and b, if b is a
subtype of a, i.e. if fn(b) and fn(a) both work [trying not to mention
contravariance], why would you expect a.__class__ and b.__class__ to
have any relation? [translate obj->class
obj.__class__->class.__metaclass__]).

-- bjorn





More information about the Python-list mailing list