identifier class enforcement

Mel Wilson mwilson at
Mon Jul 19 22:55:18 CEST 2004

In article <cdgqgi$2ab$1 at>,
"Florian Preknya" <bolo at> wrote:
>I work on building a metamodel from an UML diagram (serialized as xmi).
>There I have a class called Class that represents a class from the UML
>model. The Class has a name member and ininitially it was a string. What I
>want to do is to enfoce the class name be an identifier (for example to
>exclude white spaces etc.).
>I defined an Identifier class, having the builtin str as base class. I have
>2 problems:
>    1. The string is not initialized with the s.strip().replace(" ", "_")
>    2. I can at any time change the member type from client code,
>so my enforcement with the Identifier class is gone.
>I know Python is very weak typed, but can there is a solution for my second
>problem ? Or can I model the problem in some other way ?
>class Identifier(str):
>    def __init__(self, s = ""):
>        str.__init__(self, s.strip().replace(" ", "_"))

As far as I know, strings are immutable, so you have to set
a str descendant up using `str.__new__`.

        Regards.        Mel.

More information about the Python-list mailing list