[Python-Dev] Metaclass insanity - another use case

Greg Ewing greg@cosc.canterbury.ac.nz
Wed, 06 Nov 2002 12:12:47 +1300 (NZDT)


"Phillip J. Eby" <pje@telecommunity.com>:

> As for *why* I had inner classes, the following might be a good example:
>
>      class ModelElement(Element):
> 
>          class isSpecification(model.Field):
>              isRequired = 1
>              qualifiedName = 'Foundation.Core.ModelElement.isSpecification'
>              _XMINames = ('Foundation.Core.ModelElement.isSpecification',)
>              name = 'isSpecification'
>              referencedType = 'Boolean'

I'm not sure if it would be quite what Phillip needs, but
I've been wondering recently whether Python could benefit
from having an "instance" statement which does for instances
what the "class" statement does for classes.

The idea is you'd be able to say something like

   instance isSpecification(model.Field):
      isRequired = 1
      qualifiedName = 'Foundation.Core.ModelElement.isSpecification'

and it would be equivalent to

   isSpecification = model.Field(isRequired = 1,
      qualifiedName = 'Foundation.Core.ModelElement.isSpecification')

One of the use cases I have in mind is GUI programming, where
you frequently need to build complicated nested structures with
lots of keyword arguments to constructors. A construct like this
might help you to lay out the code more neatly and readably.

I haven't really thought through the details properly,
though.

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+