Style for modules with lots of constants

Rob Williscroft rtw at freenet.co.uk
Wed Nov 1 18:17:09 CET 2006


Paul McGuire wrote in news:XU32h.436$lx6.417 at tornado.texas.rr.com in
comp.lang.python: 

> 
> class Constants(object)
>     pass
> 
> (I guess value immutability could probably be implemented using clever
> implementations of __setattr__ and such, but is it really worth the 
> bother?).
> 
> Then I defined the context for my LEFT and RIGHT constants, which are
> being created to specify operator associativity, and then my constant
> fields as attributes of that object:
> 
> opAssoc = Constants(object)
> opAssoc.RIGHT = 0
> opAssoc.LEFT = 1
> 

This is nice, but you can cut down on some of the cruft:

class Constants( object ):
  pass
  
Constants.RIGHT = 0
Constants.LEFT = 1

## client code ...
print Constants.LEFT

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list