[ python-Feature Requests-1465406 ] Allowing the definition of constants

SourceForge.net noreply at sourceforge.net
Fri Apr 28 15:38:00 CEST 2006


Feature Requests item #1465406, was opened at 2006-04-05 23:30
Message generated for change (Comment added) made by ciw42
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1465406&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Parser/Compiler
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Chris Wilson (ciw42)
Assigned to: Nobody/Anonymous (nobody)
Summary: Allowing the definition of constants

Initial Comment:
One of the current optimizations due in v2.5 includes
constant folding of expressions, which as it stands
serves as a way of somply getting rid of a redundant
arithmetic operations and the like.

In practice, it's rare a developer would leave an
expression such as "2+3" sat in his/her code, but by
allowing the declaration of constants within a script,
it could make this new feature *much* more useful.

As an example, in a recent script I had the following
at the top, outside the main loop:

SCREEN_WIDTH=640
SCREEN_HEIGHT=480
SCREEN_RATIO=SCREEN_WIDTH/SCREEN_HEIGHT

As SCREEN_RATIO is used a number of times during my
main loop, it makes sense to pre-calculate it to avoid
the extra processing, but if the compiler were aware
that SCREEN_WIDTH and SCREEN_HEIGHT were constants, it
could optimise out the calculation and I could include
the calculation in-place.

I frequently make use of "constants" to make my code
more readable, and wonder whether there is any
performance penalty or lack of optimisation going on
due to them in fact being regular variables?

----------------------------------------------------------------------

>Comment By: Chris Wilson (ciw42)
Date: 2006-04-28 13:38

Message:
Logged In: YES 
user_id=1018283

I see your point, and it's a good example of why using
namespaces is so important, but in practice, with my
proposal in place, the code you propose simply wouldn't compile.

Assuming the compiler simply substituted the literal
"3.1415" for "pi" as I've proposed, you'd end up with
"3.1415 = 4", and a syntax error for trying to assign to a
literal value. You'd not get as far as running the code, so
in practice there'd be no issues with it running incorrectly.

Being able to declare constants is important as it allows
the compiler to make the sort of optimistations I mentioned
previously.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2006-04-10 13:55

Message:
Logged In: YES 
user_id=21627

The problem is that declaring the value assignment const
doesn't help. Consider this:

from math import pi

def area(r):
  return r*r*pi

pi = 4

print area(10)

So even though math.pi might be declared as a constant,
hard-coding its value into the function area would break
this program - the value of the variable pi might change not
change inside math, but it might change where it is imported.

----------------------------------------------------------------------

Comment By: Chris Wilson (ciw42)
Date: 2006-04-06 21:59

Message:
Logged In: YES 
user_id=1018283

I've re-opened this, as I don't feel it would be difficult
to implement or require any fundamental changes to the
parser or runtime. In fact, it would be very easy, and
potentially very useful beyond the scope of my initial
suggestion.

Appologies to rhettinger if this seems rude, but I would ask
that you give the following some consideration.

The addition of a "const" or similar compiler directive
would allow the compiler to simply do an on-the-fly
substitution for the specified value/string etc. There would
be no code analysis required, and adding this type of
functionality would carry no real overheads or further
complicate the compilation process. There would be no
changes required within the runtime.

Once substituted, the already incorporated compiler constant
folding features would then come into play.

I suppose, that what I'm suggesting is effectively a basic
pre-compiler macro feature. This in itself may prove useful
in many other situations.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2006-04-05 23:57

Message:
Logged In: YES 
user_id=80475

Python is too dynamic for this kind of optimization to be 
done automatically.  If those "constants" are defined at 
the module level, they can be changed by code outside the 
module.  Even within the module, it would take a thorough 
analysis of the code to determine that nothing was trying 
to alter the value of the global variable.  If 
the "constant" is defined inside a function, it is still a 
local variable subject to change by later lines in 
function.

Your best bet is to use the bind_consts recipe at ASPN.  
It will automatically turn some global references into 
locals:  
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277
940

It may be possible to adapt the recipe to go an additional 
step and fold the globals "constants".

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1465406&group_id=5470


More information about the Python-bugs-list mailing list