[Python-Dev] metaclass insanity

Walter Dörwald walter@livinglogic.de
Wed, 06 Nov 2002 11:48:59 +0100


Guido van Rossum wrote:
>> > Can someone provide a reason why you'd want to use nested classes?
>> > I've never felt this need myself.  What are the motivations?
>>
>>XIST (http://www.livinglogic.de/Python/xist/) uses nested classes
>>to map XML element types and their attributes to Python classes.
>>For example the HTML element type img looks like this in XIST:
>>
>>class img(Element):
>>    class Attrs(Element.Attrs):
>>       class src(URLAttr): required = True
>>       class alt(TextAttr): required = True
>>       class align(TextAttr): values = ("top", "middle", ...)
> 
> 
> That's cool.

But the __name__ problem becomes apparent for error handling.

Checking whether required attributes are specified is the
job of the nested Attrs class. The error message should include
the name of the affected element, but this name is not available
in the inner class (but see my workaround in the posting with
the __outerclass__/__fullname__ example.)

Bye,
    Walter Dörwald