Defining classes

Nick Maclaren nmm1 at cus.cam.ac.uk
Wed Dec 13 17:51:22 EST 2006


In article <Xns9898DF2734FF2duncanbooth at 127.0.0.1>,
Duncan Booth <duncan.booth at invalid.invalid> writes:
|> > 
|> > 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)
|> 
|> You cannot refer to weeble until it has been created which isn't until 
|> after all of the statements in the class body have executed. The usual way 
|> to achieve what you want is to assign the static member from outside the 
|> class.
|> 
|> class weeble:
|>     pass
|> 
|> weeble.wumpus = brinjal(weeble)

Thanks (and to Gabriel Genellina).  That is what I am doing, but it is
not ideal in other respects - for example, I want to make that item
read-only!  It would be much cleaner not to have to fiddle with static
members after the class is initialised.

|> Alternatively you can play tricks with metaclasses for a similar effect.

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 :-)

What I am trying to do is very simple, but is somewhat unusual.  That
is the story of my life, so I am used to having problems.


Regards,
Nick Maclaren.



More information about the Python-list mailing list