What do you use as symbols for Python ?

Daniel Evers daniel.evers at rwth-aachen.de
Fri Nov 11 13:26:16 EST 2005


Hi!

Never would have thought of this...
I mixed this with the class-version and created a new class derived from
"str" for easier printing and added an iterator:

---

class Enum:
        class Type(str):
                def __init__(self, name):
                        self.__name = name
                def __str__(self):
                        return self.__name

        def __init__(self, *keys):
                self.__keys = []
                for key in keys:
                        mytype = self.Type(key)
                        self.__dict__[key] = mytype
                        self.__keys.append(mytype)
                self.__index = -1
                self.__count = len(keys)

        def __iter__(self):
                return self

        def next(self):
                self.__index = self.__index + 1
                if (self.__index >= self.__count):
                        self.__index = -1
                        raise StopIteration
                return self.__keys[self.__index]

friends = Enum("Eric", "Kyle", "Stan", "Kenny")
print "These are my friends:",
print ", ".join([kid for kid in friends])
for kid in friends:
        print kid,
        if kid is friends.Kenny:
                print "dead"
        else:
                print "alive"
---

Daniel



More information about the Python-list mailing list