Nested class structures
John Lenton
john at grulic.org.ar
Sat Sep 11 08:50:33 EDT 2004
On Sat, Sep 11, 2004 at 12:28:44AM +0000, OKB (not okblacke) wrote:
> 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.
Two things. First, I don't understand (you don't explain) your
requirement for the classes to be 'anonymous'; the example in the dict
can be made easily, with
class a:
one = 1
class nested:
two = 2
three = 3
class nestnest:
.
.
.
but obviously there's something more to it.
Second, a class is an object like any other; you can include it in a
dictionary (that's all a nested class is doing, really, with some
sugar to get at it in convenient ways), push it into a list,
whatever. They're even hashable, so you can use them as dictionary
keys.
--
John Lenton (john at grulic.org.ar) -- Random fortune:
Don't tell any big lies today. Small ones can be just as effective.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20040911/fcdd0943/attachment.sig>
More information about the Python-list
mailing list