inline metaclasses
Alex Martelli
aleax at mac.com
Mon Jul 3 20:50:00 EDT 2006
Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> In <1151952177.002598.40850 at b68g2000cwa.googlegroups.com>, gangesmaster
> wrote:
>
> > just something i thought looked nice and wanted to share with the rest
> > of you:
> >
> >>>> class x(object):
> > ... def __metaclass__(name, bases, dict):
> > ... print "hello"
> > ... return type(name, bases, dict)
> > ...
> > hello
> >>>>
> >
> > instead of defining a separate metaclass function/class, you can do
> > it inline. isn't that cool?
>
> But why use a metaclass? If the meta class is only applied to *one*
> class, can't you do at class level whatever the metaclass is doing!?
Most but not all of the "whatever". E.g.:
class X:
class __metaclass__(type):
def __str__(cls): return 'The great class X!'
print X
You can't make "print X" behave arbitrarily w/o a custom metaclass.
Alex
More information about the Python-list
mailing list