[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Python enumerations



On 7/19/05, Dan <dan@xxxxxxxxxxxxxxxx> wrote:
> The only problem I'm encountering is the stringification of classes:
> 
> >>> class A: pass
> ...
> >>> x = A
> >>> print str(x)
> __main__.A

You can affect the stringification of classes via metaclasses. For example:

class StrMeta(type):
    def __str__(self):
        return self.__name__

class A(object):
    __metaclass__ = StrMeta

>>> x = A
>>> print x
A

The __metaclass__ attribute is inherited, so you won't need to define
it in every enum class. E.g.

class Enum(object):
    __metaclass__ = StrMeta

class A(Enum): pass

-- 
Sami Hangaslammi