having both dynamic and static variables
Carl Banks
pavlovevidence at gmail.com
Sun Mar 6 00:59:55 EST 2011
On Mar 5, 7:46 pm, Corey Richardson <kb1... at aim.com> wrote:
> On 03/05/2011 10:23 PM, MRAB wrote:
>
> > Having a fixed binding could be useful elsewhere, for example, with
> > function definitions:
> > [..]
> > fixed PI = 3.1415926535897932384626433832795028841971693993751
>
> > fixed def squared(x):
> > return x * x
>
> This question spawns from my ignorance: When would a functions
> definition change? What is the difference between a dynamic function and
> a fixed function?
There's a bit of ambiguity here. We have to differentiate between
"fixed binding" (which is what John Nagle and MRAB were talking about)
and "immutable object" (which, apparently, is how you took it). I
don't like speaking of "constants" in Python because it's not always
clear which is meant, and IMO it's not a constant unless it's both.
An immutable object like a number or tuple can't be modified, but the
name refering to it can be rebound to a different object.
a = (1,2,3)
a.append(4) # illegal, can't modify a tuple
a = (1,2,3,4) # but this is legal, can set a to a new tuple
If a hypothetical fixed binding were added to Python, you wouldn't be
able to rebind a after it was set:
fixed a = (1,2,3)
a = (1,2,3,4) # now illegal
If you could define functions with fixed bindings like this, then a
compiler that's a lot smarter than CPython's would be able to inline
functions for potentially big speed increases. It can't do that now
because the name of the function can always be rebound to something
else.
BTW, a function object is definitely mutable.
def squared(x):
return x*x
squared.foo = 'bar'
Carl Banks
More information about the Python-list
mailing list