Question about accessing class-attributes.

Michele Simionato mis6 at pitt.edu
Fri Apr 25 14:30:21 EDT 2003


Michael Hudson <mwh at python.net> wrote in message news:<7h37k9ilm5v.fsf at pc150.maths.bris.ac.uk>...
> Thomas Heller <theller at python.net> writes:
> 
> > Michael Hudson <mwh at python.net> writes:
> > 
> > > mis6 at pitt.edu (Michele Simionato) writes:
> > > 
> > > > The meta-type conflict is rather annoying, indeed. According to the
> > > > metaclass book, the language should generate the correct metaclass
> > > > that solves the conflict automatically, without burden for the
> > > > programmer. Python is not that magic (yet) therefore you have to
> > > > solve the conflict yourself.
> > > 
> > > I guess a potential problem is that if you do
> > > 
> > > class C(object):
> > >     __metaclass__ = A
> > > 
> > > class D(object):
> > >     __metaclass__ = B
> > > 
> > > class E(C, D):
> > >     pass
> > > 
> > > class F(C, D):
> > >     pass
> > > 
> > > you'd like both E and F to have the *same* (rather than merely
> > > equivalent) metaclasses.  This would seem to be tricky to arrange.
> > 
> > Wouldn't a caching class factory be able to do this?
> 
> Yes.  I only said "tricky", not impossible.
> 
> class MetaGit(type):
>     def __eq__(self, other):
>         # aha! this makes class objects unhashable, cackle!
>         return self is other
> 
> I don't know whether you want to cope with things like that.
> 
> Cheers,
> M.

Two questions:

1) I don't see how this solve the problem of avoiding duplications of
metaclasses;

2) why you say this makes classes unhashable ?

It seems I can still use A and B as keys for a dictionary: 

class A: __metaclass__ = MetaGit
class B: __metaclass__ = MetaGit

dic={A:1, B:2}

print dic[A]  #=>1 okay
print dic[B]  #=>2 okay

?? Maybe I am missing something obvious ...


                       Michele




More information about the Python-list mailing list