Equivalent to C bitmasks and enumerations

John Machin sjmachin at lexicon.net
Wed Apr 15 21:08:50 EDT 2009


On Apr 16, 10:13 am, Dave Angel <da... at ieee.org> wrote:
>
> For the Color example, how's this for a starting place:
>
> class Color(object):
>     def __init__(self, name, enum):
>         self.enum = enum
>         self.name = name
>         setattr(Color, name, self)
>
>     @staticmethod
>     def seal():
>         del Color.__init__
>         del Color.seal
>
>     def __str__(self):
>         return self.name
>
> Color("RED", 4)
> Color("GREEN", 11)
> Color.seal()        #prevent any new instances from being created
>
> b = Color.RED
> print type(b)
> print str(b)
>
> a = Color.GREEN
> print a

So when you use Color.GREEN in an expression or pass it as a function/
method argument, it produces "GREEN" ... doesn't emulation of a C-
style enum require it to produce 11?

> print a.enum




More information about the Python-list mailing list