What do you use as symbols for Python ?

Gary Herron gherron at digipen.edu
Thu Nov 10 12:27:14 EST 2005


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
>  
>
Another similar approach that keeps those values together in a single 
namespace is this (my favorite):

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

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

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

Gary Herron



-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418





More information about the Python-list mailing list