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

Kent Johnson kent37 at tds.net
Thu Jan 6 12:50:40 CET 2005


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.

> 
> 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'

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!!

>     # boo, we have no builtin bool
>     global True
>     global False
>     True = 1
>     False = 0

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

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.

# Constants.py
MINVERSION = 1.5

# Client.py
import Constants

if (Constants.MINVERSION == 1.5):
   ...

or, if you want to import all the constants directly,

# Client2.py
from Constants import *

if (MINVERSION == 1.5):
   ...

> 
> A last question would also be if the equivalent of __FILE__ and __LINE__ 
> macros exist?

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
> 


More information about the Tutor mailing list