having both dynamic and static variables

BartC bc at freeuk.com
Wed Mar 9 13:23:22 EST 2011



"Steven D'Aprano" <steve+comp.lang.python at pearwood.info> wrote in message
news:4d743f70$0$29984$c3e8da3$5496439d at news.astraweb.com...
> On Sun, 06 Mar 2011 12:59:55 -0800, Westley Martínez wrote:
>
>> I'm confused. Can someone tell me if we're talking about constant as in
>> 'fixed in memory' or as in 'you can't reassign' or both?
>
> Python already has fixed in memory constants. They are immutable objects
> like strings, ints, floats, etc. Once you create a string "spam", you
> cannot modify it. This has been true about Python forever.
>
> What Python doesn't have is constant *names*. Once you bind an object to
> a name, like this:
>
> s = "spam"
>
> you can't modify the *object*, but you can rebind the name:
>
> s = "Nobody expects the Spanish Inquisition!"
>
> and now your code that expects s to be "spam" will fail. So the only new
> feature under discussion is a way to bind-once names, which many people
> call constants.

Another example:

pi=3.141592654

print ("pi is:",pi)

pi=42

print ("pi is now:",pi)

which is clearly undesirable.

Many languages just don't 'get' constants; C is one (the closest it comes is
'#define' and 'enum', while what it calls 'const' is really 'read-only
variable'), and perhaps Python is another.

But then, the dividing line between constants and 'variables' can get 
confused when the values involved are complex (strings and such), so might 
be understandable up to a point.


-- 
bartc 




More information about the Python-list mailing list