"Rob Williscroft" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Paul McGuire wrote in news:[EMAIL PROTECTED] 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.

One man's cruft is another man's clarity.  The reason I used instances 
instead of just the Constants class was so that I could define a little more 
descriptive context for the constants, rather than simply a shortcut for 
importing lots and lots of constant definitions.  I expect I will want to 
define some more constants for other parts of pyparsing, and the instance 
scoping helps organize them, that's all.  It's really a style/taste issue at 
this point.  What you've written will certainly work, and if Neil has many 
constants in his module, what you suggest would be a way to import them all 
at once.

-- Paul


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to