nesting class definitions?

Aahz Maruch aahz at netcom.com
Sun Jan 30 13:27:41 EST 2000


In article <20000128182031.C16093 at xs4all.nl>,
Thomas Wouters  <thomas at xs4all.net> wrote:
>
>Nesting doesn't really *do* anything in python, except bind the class to a
>different namespace. And there is no difference between classes, modules,
>functions, lists, dictionaries or any of the other objects. For instance:
>
>class Viking:
>	class Spam:
>		pass
>
>is effectively equivalent to:
>
>class private_Spam:
>	pass
>
>class Viking:
>	Spam = private_Spam

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)

You can now have multiple instances of the Spam *class*, each with
different values for the class-level variables.  I think this is what
people are often trying for when they nest things in Python; you just
need an additional level of nesting in order to generate the multiple
namespaces.

(Yeah, this is essentially meta-class stuff, which I know you know
about, but I think presenting it slightly differently like this will
make sense to more people.  It certainly makes more sense to me --
metaclasses usually make my head hurt.  ;-)
--
                      --- 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