Defining classes

Steven Bethard steven.bethard at gmail.com
Thu Dec 14 14:52:10 EST 2006


Nick Maclaren wrote:
> I am defining a class, and I need to refer to that class when
> setting up its static data - don't ask - like this:
> 
> Class weeble :
>     wumpus = brinjal(weeble)

Duncan Booth wrote:
> Alternatively you can play tricks with metaclasses for a similar effect.

Nick Maclaren wrote:
> Well, I am already doing that, and regretting the fact that Python
> doesn't seem to allow a class instantiation to return a new class :-)

How are you doing it currently?  Here's a sort of minimalist option:

     >>> class weeble(object):
     ...     def __metaclass__(name, bases, bodydict):
     ...         cls = type(name, bases, bodydict)
     ...         cls.wumpus = 'brinjal', cls
     ...         return cls
     ...
     >>> weeble.wumpus
     ('brinjal', <class '__main__.weeble'>)

Of course, it still takes four lines and a metaclass...

STeVe



More information about the Python-list mailing list