Metaclasses broke in 2.2?

Drew Csillag drew_csillag at geocities.com
Tue Aug 14 18:06:36 EDT 2001


On Tue, Aug 14, 2001 at 03:06:44PM -0400, Guido van Rossum wrote:
> > From: Drew Csillag <drew_csillag at geocities.com>
> 
> > Another question is: will you be able to inherit from C and get the same
> > behavior as the current situation where ideally this would work:
> > 
> > class meta:
> >     def __init__(self, klass, bases, dict):
> >         pass
> > 
> > class foo:
> >     __metaclass__ = meta
> > print 'foo is', foo  # foo *is* an meta instance
> > 
> > class bar(foo): pass # dies here
> 
> This dies because your meta is a classic class.  If you make it a
> new-style class by inheriting from object, it can be made to work by
> defining a __new__ method on the meta class.
> 
> But normally you would inherit meta from type, wouldn't you?

I hadn't thought to.  I was originally just trying to check whether
some already existing code (PyDO specifically) would work with 2.2 and
found out that it died very badly.

> > print 'bar is', bar  # would be lovely if bar was an instance of meta
> 
> Yes, this works.
> 
> Here's a small example that works in current CVS:
> 
>     class meta(object):
> 	def __new__(cls, name, bases, dict):
> 	    return object.__new__(cls)
> 	def __init__(self, name, bases, dict):
> 	    self.name, self.bases, self.dict = name, bases, dict
> 	def __repr__(self):
> 	    return "<meta instance %s>" % self.name
> 
>     class foo:
> 	__metaclass__ = meta
> 
>     print foo
> 
>     class bar(foo):
> 	pass
> 
>     print bar

Ahhh, perfect!  Exactly what I was looking for.

..snip..

> > I guess what I'm really trying to say is that for a library I
> > currently have using 2.1, currently the user can be totally ignorant
> > of the fact that they are not defining regular classes, but
> > metaclasses, and ideally, I'd like to keep it that way.
> 
> If you can smuggle a "__metaclass__ = type" into their namespace, you
> can get the same effect.

Actually, since they inherit the metaclass as a base class e.g.:
  class myDataClass(PyDO.PyDO):
I don't have to resort to such smugglery.

Now to try and figure out why (I've heard about this stuff) my __getattr__
function is going into recursive death.

Thanks,
Drew
-- 
print(lambda(m,d,y):['Sun','Mon','Tues','Wed','Thurs','Fri','Satur'][(
lambda(m,d,y):(23*m/9+d+4+y/4-y/100+y/400)%7)(m<3and(m,d+y,y-1)or(m,
d+(y-2),y))])(map(int,raw_input('mm/dd/yyyy>').split('/')))+'day'




More information about the Python-list mailing list