Nested class structures

OKB (not okblacke) BrenBarn at aol.com
Fri Sep 10 20:28:44 EDT 2004


Larry Bates wrote:

> It is a little hard to understand what you want but you
> may just want to put class instances in a list.
> 
> class anon:
>     pass
> 
> class class1:
>     def __init__(self):
>       self.l=[anon(), anon(), anon()]

    	Yeah, sorry, I should have been a little more clear.  Anyway, it's 
important that these be actual class definitions, because I want the 
ability to include per-class methods and stuff.  So I don't really want 
a bunch of identical anonymous classes.  I want something like:

class class1:
    	class anon:
    	    	def meth1(self): print "meth1"
   	class anon:
    	    	def meth1(self): print "different meth1"
    	class anon:
    	    	def meth1(self): print "another meth1"

    	This is why I want to define the classes inline, nested in the 
other class.  I'm trying to leverage the syntactic structure of class 
definitions to get nested structures of code.  It's somewhat akin to

a = { 'one': 1,
    	'nested': { 'two': 2, 'three': 3,
    	    	'nestnest': { 'four': 4, 'five': 5 }
    	}
    	'othernest': { 'six': 6 }
    }

    	. . . except that I want the ability to include arbitrary python 
code where I have 1, 2, 3 there (as dictionary values).  As far as 
I can tell, Python doesn't provide a way to define code as part of 
a larger expression like this, except in a class definition.

-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown



More information about the Python-list mailing list