[Python-3000] yes to class decorators
Josiah Carlson
jcarlson at uci.edu
Fri Nov 17 07:33:47 CET 2006
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>
> tomer filiba wrote:
>
> > we should also note that it's possible to replace "__metaclass__"
> > by a @metaclass decorator.
>
> I don't think so. Your proposed technique for that seems to
> involve creating an ordinary class first, then taking it
> apart and recreating it using the specified metaclass.
> That smells very wrong to me.
I more or less agree, at least on the side of 'creating and destroying a
class is a waste', but here's some abuse that I couldn't help chuckling
over. No need to even bother constructing a class, but don't forget that
'return locals()' at the end! (not that I'm advocating its [ab]use,
just something that made me smile)
- Josiah
>>> def metaclass(*bases):
... def class_factory(fcn):
... return type(fcn.__name__, bases, fcn())
... return class_factory
...
>>> @metaclass(object)
... def newclass():
... def foo(self, arg):
... print arg
... return locals()
...
>>> newclass().foo('hello!')
hello!
>>>
More information about the Python-3000
mailing list