Rob Williscroft:
> 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
Another possibility is to define such constants as strings instead of
integers:
_allflags = ("left",
"right",
# other constants...
# other constants...
)
class Flags(): pass
for _flag in _allflags:
setattr(Flags, _flag, _flag)
#print Flags.__dict__
Then the user can use Flags.left+" "+Flags.... , or just the "left"
string.
Then maybe for each attribute like the "left" constant, a "left_doc"
attribute can be added to Flags with a textual explanation of the
meaning of the "left" constant.
If your module has a short name, then maybe it's better to just use
such string constants as modulename.constant instead of Flags.constant,
using globals()[_flag] = _flag
Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list