Style for modules with lots of constants

Ron Adam rrr at ronadam.com
Wed Nov 1 23:19:13 CET 2006


Neil Cerutti wrote:
> On 2006-11-01, bearophileHUGS at lycos.com <bearophileHUGS at lycos.com> wrote:
>> Neil Cerutti:
>>> scriptref = glk.fileref_create_by_prompt('Transcript+TextMode',
>>>    'WriteAppend', 0)
>> That "+" sign seems useless. A space looks enough to me. The
>> functions can accept case-agnostic strings and ignore spaces
>> inside them. Example:
>> ('transcript textmode ', 'writeappend', 0)
> 
> That's pretty cool. Not as pretty, but easier for users,
> possibly.
> 
>>> Parsing the combinable bitfield contants might be slowish,
>>> though.
>> I think you have to test if this is true for your situation.
>> Management of interned strings is probably fast enough
>> (compared to the rest of things done by the Python interpreter)
>> for many purposes.
> 
> Agreed. I meant I'd have to later test if it's were fast enough.

I've found using plain strings as flags has several advantages.

The value 'textmode' is always 'textmode', there is no dependency on a module or 
class being imported or initialized.

It avoids having to type namespace prefixes such as thismod.textmode, or 
myclass.textmode or importing a bunch of values into global name space.

Strings comparisons in python are very fast.

The disadvantage is an invalid flag may pass silently unless you do some sort of 
validation which may slow things down a bit.

Ron






More information about the Python-list mailing list