Global variables for python applications

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon May 17 05:13:35 EDT 2010


On Mon, 17 May 2010 19:56:15 +1200, Gregory Ewing wrote:

> John Nagle wrote:
>> Also, more compile-time arithmetic becomes possible.
> 
> But only if their values can be computed at compile time.

John said "more", not "everything imaginable can be calculated at compile 
time" :)

Python already does constant folding at compile time:


>>> code = compile('"abc"*2*3', '', 'single')
>>> dis.dis(code)
  1           0 LOAD_CONST               5 ('abcabcabcabcabcabc')
              3 PRINT_EXPR
              4 LOAD_CONST               3 (None)
              7 RETURN_VALUE



> This leads to
> a huge can of worms if you want to be able to import named constants
> from other modules.


Why? Once the module is loaded, the named constant is bound to an object. 
Provided that it can't be rebound or mutated, where's the can of worms?


> A large part of what currently happens only at run
> time would have to become possible at compile time as well. Either that
> or so many restrictions would have to be placed on the way that the
> values of named constants are specified that they would not be very
> useful in practice.

I disagree. Enforcing immutability would be tricky, but enforcing once-
only name binding is relatively simple. There's even a recipe for it in 
the Python Cookbook.




-- 
Steven



More information about the Python-list mailing list