nesting class definitions?

Aahz Maruch aahz at netcom.com
Mon Jan 31 15:59:47 EST 2000


In article <20000131144411.A19725 at xs4all.nl>,
Thomas Wouters  <thomas at xs4all.net> wrote:
>On Sun, Jan 30, 2000 at 06:27:41PM +0000, Aahz Maruch wrote:
>> Yup.  But try this one on for size:
>>
>> def SpamGen ( foo ):
>> 	class Spam:
>> 		bar = foo
>> class Viking:
>> 	def __init__(self, foo):
>> 		self.spamClass = SpamGen(foo)
>
>Er, correct me if i'm wrong, but shouldn't this generate a NameError (asside
>from SpamGen returning None, of course) ? Or did you intend to show that
>this is very clear and very useful code, but impossible at the moment ?

Oops.  That's what I get for assuming that something will work without
testing.  I've figured out how to make it work using "global" but that's
really ugly.  For the curious:

def SpamGen ( foo ):
	global plugh
	plugh = foo
	class Spam:
		bar = plugh
	return Spam
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Have a *HAPPY* day!!!!!!!!!!



More information about the Python-list mailing list