Style for modules with lots of constants
Scott David Daniels
scott.daniels at acm.org
Thu Nov 2 23:19:31 EST 2006
Paul McGuire wrote:
> class Constants(object):
> pass
>
> 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
I like something like:
class Constants(object):
def __init__(self, **kw):
for name, val in kw.iteritems():
setattr(self, name, val)
Then:
opAssoc = Constants(RIGHT=0, LEFT=1)
> In your example, this would look something like:
>
> fileusage = Constants()
> fileusage.Transcript = 1
> fileusage.TextMode = 2
fileusage = Constants(Transcript=1, TextMode=2)
filemode = Constants(Read=1, Write=2, Append=4)
filemode.WriteAppend = filemode.Write | filemode.Append
class Constants then becomes a nice place to define methods
to convert values to associated names for debugging and such.
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list