Hey all,
I've done the usual googling, checked the Learning Python book and did some list searches, to no avail as of yet.
I'm _very_ used to using C style constants (preprocessor #define directives) or C++ const keyword style, for a variety of reasons.
I've yet to see anything covering 'how to work around the lack of constants in Python'...can anyone point me in the right direction here?
A few examples/reasons for use:
The 'need' to define a global constant in an imported module, for example- (I know about sys.version_info, but it doesn't exist in 1.5.2...don't ask ;-) I also know this could be handled via a class, but what is the equivalent of the following snippets? Not so interested in style comments (I am, but not on these/this thread ;-) as much as other ways to do this..
1. ourversion.py:
import sys
MINVERSION = 1.5
def checkVersion(): """ used as an evil hack because 1.5.2 doesn't have sys.version_info """
# Not sure why, but declaring MINVERSION as repr(foo) in the # first place doesn't work, something about python type handling # I'm sure.. return repr(MINVERSION) >= repr(getVersion())
def getVersion(): return sys.version[0:3]
if repr(getVersion() < 2.0) # boo, we have no builtin bool global True global False True = 1 False = 0
funkyScopeAndConstants.py: import ourversion.py import sys import os import <someOtherModule>
... ... if someOtherModule.property = True # do something
You get the point.
The other oddity is without being able to define a 'real' constant, as in #DEFINE MINVERSION 1.5,
the scope of MINVERSION (and True/False) even using the global keyword still uses the ocal file's namespace. I don't want to debate the merits of using globals...most people that claim they never use any in other languages _still_ use constants in header files, which is the purpose I'd like to be able to do generally....not to mention the fact that I really am _not_ thrilled with the use of string literals typed in each time in code (yes, quick and dirty code but still) I see seems to be 'OK' in python for the use of comparisons...opposed to something like
if(strncmp(strVal, CONSTANT_STRING_VAL_LIKE_HTTP_ACCEPT_ENCODING_HEADER, strlen(strVal) { do_something(); }
or if(floatVal > PI) { do_something() }
ok, hopefully that's explaining some of why I'd like a 'constant equivalent' as well as a question on global scoping/python namespaces.
A last question would also be if the equivalent of __FILE__ and __LINE__ macros exist?
Thanks,
Scott
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor