Style for modules with lots of constants

Tim Chase python.list at tim.thechases.com
Wed Nov 1 15:28:01 EST 2006


>> 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,
> 
> Sorry I don't know what you mean here, could I have an example


It helps in the recognition if you have separation between 
something like

	turnDirection.LEFT

and

	alignment.LEFT

They may be the same or different value for LEFT, but by grouping 
them in a "descriptive context", it's easy to catch mistakes such as

	paragraph.align(turnDirection.LEFT)

when what you want is

	paragraph.align(alignment.LEFT)

and, as an added bonus, prevents name clashes such as

	turnDirection.LEFT = 2
	alignment.LEFT = 0

With the grab-bag o' constants, you have to use old-school 
C-style in your modern name-space'd Python:

	gboc.TURN_LEFT = 2
	gboc.ALIGN_LEFT = 0

or even worse, you'd end up with cruftage where you have

	gboc.LEFT = 2
	gboc.ALIGN_LEFT = 0 #created a month later as needed

and then accidentally call

	paragraph.align(gboc.LEFT)

when you mean

	paragraph.align(gboc.ALIGN_LEFT)

This is what I understand the grandparent's post to be referring 
to by "descriptive context".

-tkc






More information about the Python-list mailing list