names

Alex Martelli aleaxit at yahoo.com
Tue Jan 16 08:59:23 EST 2001


"Elliott Chapin" <echapin at sympatico.ca> wrote in message
news:trL86.74752$n%.3041493 at news20.bellglobal.com...
> Thanks, all! I may approach the actual task I have in mind by designing a
> class which adds a name to the attributes of its instances.

Excellent idea, and probably also best for the parallel thread
"determining names of instances" started by Daniel Klein.  You
will have to pass (e.g.) name='NiceFoo' to the constructor, of
course, unless you want to have it assign 'random' names...:

def Named:
    unnamed=0
    def __init__(self, name=None):
        if name is None:
            self.name = "Unnamed_%d" % Named.unnamed
            Named.unnamed += 1
        else:
            self.name = name

You can use this as a mix-in class (inherit your classes from
it), as long as you remember to call its __init__ appropriately
from your own __init__ (passing along the name-argument...).


Alex







More information about the Python-list mailing list