[Python-ideas] adopt an enum type for the standard library?

Aaron Brady castironpi at comcast.net
Thu Jan 24 21:57:42 CET 2008


> Something like "wheres= Enum( UP= 0, DOWN= 0, LEFT= 0, RIGHT= 0 )" is
> easily written as:
> 
>     class wheres:
          @staticmethod
          def reassign():
              wheredir= [ v for v in dir( wheres ) if
                  not callable( getattr( wheres, v ) ) and
                  not v.startswith( '_' ) ]
              for i, v in enumerate( wheredir ):
                  setattr( wheres, v, i )
>         UP = 0; DOWN = 0; LEFT = 0; RIGHT = 0
      wheres.reassign()

We typically reassign enum values once they're assigned, and sometimes just
add new ones.

I tend to write new classes where I think people use enums.  20% longer
code; 80% fewer ifs. [1]

[1] http://en.wikipedia.org/wiki/Visitor_pattern





More information about the Python-ideas mailing list