identifier class enforcement

Christopher T King squirrel at WPI.EDU
Tue Jul 20 02:37:15 CEST 2004

On Mon, 19 Jul 2004, Mel Wilson wrote:

> In article <cdgqgi$2ab$1 at>,
> "Florian Preknya" <bolo at> wrote:
> >    1. The string is not initialized with the s.strip().replace(" ", "_")
> >expression.
> >
> >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__`.

I've had to do this before.  Using __new__, what you want to do is:

class Identifier(str):
    def __new__(cls, s = ""):
        return str.__new__(cls, s.strip().replace(" ", "_"))

As Mel pointed out, strings are immutable, so you have to set their value 
before they are created (i.e. create them with a value).

> >    2. I can at any time change the member type from client
> > code, so my enforcement with the Identifier class is gone.

You can enforce this in Class using properties:

class Class(object):	# must be new-style class for this to work
    def getname(self):
        return self._name

    def setname(self,v):
        if not isinstance(v,Identifier):
            raise TypeError, 'name must be of type Identifier!'
	self._name = v

    name = property(setname,getname)

Now, any assignment to or retrieval from will transparently go 
through Class.getname() and Class.setname().

However, as mentioned by Jeff, a function Identifier() might be a bit 
neater than a class.  In this case, you can force name to be of type 
Identifier by defining setname() like this:

    def setname(self,v):
	self._name = Identifier(v)

This will also work if you use the class approach.

Depending on your application, it may be better just to use setname() and
getname() directly, and forgo use of  Some think this is
better OO methodology all around, while others think properties are 
cleaner in certain instances.  I'll let you be the judge ;)

Hope this helps.

More information about the Python-list mailing list