What do you use as symbols for Python ?

Chris Smith smitty_one_each at bigfoot.com
Sat Nov 12 03:49:31 EST 2005


>>>>> "Gary" == Gary Herron <gherron at digipen.edu> writes:

    Gary> Erik Max Francis wrote:
    >> Pierre Barbier de Reuille wrote: > > >
>>When you need some symbols in your program, what do you use in
    >> Python ?
    >>> 
    >>> For example, an object get a state. This state is more
    >>> readable if expressed as a symbols, for example "opened",
    >>> "closed", "error".  Typically, in C or C++, I would use an
    >>> enum for that: enum OBJECT_STATE { opened, closed, error
    >>> }
    >>> 
    >>> 
    >>
> OPENED, CLOSED, ERROR = range(3)
    >> 
    >> object.state = OPENED
    >> 
    >> 
    Gary> Another similar approach that keeps those values together in
    Gary> a single namespace is this (my favorite):

    Gary>   class State: OPENED, CLOSED, ERROR = range(3)

    Gary> Then you can refer to the values as State.OPENED
    Gary> State.CLOSED State.ERROR

    Gary> The extra clarity (and slight wordiness) of the dotted
    Gary> notation seems, somehow, quite Pythonic to me.

    Gary> Gary Herron

I think Zoran Isailovski has the last word on the topic:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413486




More information about the Python-list mailing list