[Tutor] replacement for constants from other languages in Python?

Scott W wegster at mindcore.net
Thu Jan 6 13:50:38 CET 2005


Kent Johnson wrote:
> Scott W wrote:
> 
>> 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
> 
> 
> This is the usual Python way. Python's approach generally is 'treat your 
> users like adults', i.e. give them the information needed to make 
> sensible decisions, but don't try to keep them from doing something you 
> think is stupid. Putting the name in upper case gives the information 
> that this is intended to be a constant.

Sure, if I can't make it a true constant value, might as well make it
'obvious' ;-)

>>
>> 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..
> 
> 
> MINVERSION = repr(1.5)
> should work just fine. It will give the same result as the more readable
> MINVERSION = '1.5'

Ok, this would make a bit more sense RE: repr()- in one of the resources
I found, it seemed to state that repr(x) was converting x into a numeric
representation, ala atoi() and friends.  Obviously, this is the opposite
of what I'd actually wanted.  The sample snippets I put in had a
duplicated repr() if not more than one, which wasn't doing what I
expected anyways (although at which point I was essentially comparing
strings of similar length, getting 'a' result, just not the intended one ;-)


> I'm not sure why you are using repr() so much.
> 
>>     return repr(MINVERSION) >= repr(getVersion())
>>
>> def getVersion():
>>     return sys.version[0:3]
>>
>> if repr(getVersion() < 2.0)
> 
> 
> This will certainly not do what you want for two reasons.
> - getVersion() returns a string, you are comparing it to a float which 
> will not give a meaningful result.
> - In Python < 2.3 the result of the comparison will be an integer 0 or 
> 1. repr() converts this to a *string* '0' or '1' which will *always* 
> evaluate as True!!

Yep, see above...wish I knew where I saw that 'explanation' of repr()
and str()

>>     # boo, we have no builtin bool
>>     global True
[snip]

> Rather than testing the version, you can test directly to see whether 
> True and False are defined. If not, you can add them to the __builtin__ 
> module and they will be globally available. Here is one way:
> 
> import __builtin__
> if not __builtin__.hasattr('True'):
>    __builtin__.True = 1
> if not __builtin__.hasattr('False'):
>    __builtin__.False = 0

OK, that's helpful.  I can see the advantages of several of python's
mechanisms, such as dir() as well as treating everything like an
'interactive object to determine existing methods and attributes (dir()
and your example of hasattr().  Pretty cool...now to go from libc/POSIX
to an entirely new and (mostly) different set of core libs and
functionality.. ;-)  Actually, that IS pretty nice, can perhaps take the
place of some(most?) of features.h, unistd.h and friends....which is
really what I want, regardless of the example...or having to 'roll my
own'.  Very cool.

> This suggestion is taken from this thread on comp.lang.python:
> http://tinyurl.com/46me3
> 
> Note that adding names to __builtin__ is NOT recommended in general! 
> Don't use this to create your own global constants! It is OK in this 
> case because to duplicate Python 2.3 behaviour you need a true global.
> 
>> 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
> 
> 
> The usual Python solution is to make a module containing constant 
> definitions and import it where you need them.
> 
[snip]

> or, if you want to import all the constants directly,
> 
> # Client2.py
> from Constants import *

There was my catch/issue I was having.  I expected the global keyword
being used in my previous bool/True/False definition to put it into a
global namespace, but I still had a namespace issue as you explained
here- simply doing an 'import <module>' will allow access to variables
created as globals in that module, but not without scope resolution, ie
module.True, module.MINVERSION, etc...what I expected was anything
declared as global to simply be in the global namespace of any other
module importing the one with the declaration.

> if (MINVERSION == 1.5):
>   ...
> 
>>
>> A last question would also be if the equivalent of __FILE__ and 
>> __LINE__ macros exist?
> 

Great- looks like there's a link for python < 2.X as well, will
definitely look at using that!

Thanks!

> See this recipe and the one linked to in its discussion:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
> 
> Kent
> 
>>
>> Thanks,
>>
>> Scott
>>
>>     _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 





More information about the Tutor mailing list