having both dynamic and static variables

P

Paul Rubin

Steven D'Aprano said:
L[0].append(5) # mutate L, in some reasonable sense of "mutate"

You haven't mutated the tuple called "L". You've mutated its internal
components, which are lists. If you wanted them to also be immutable, you
should have used tuples :)

Obviously you are correct in one reasonable sense of "mutate". In
another sense, if L is immutable and you evaluate a purely computational
function on it two different times, you should get the same result both
times; for example, if you pickle L twice you should get two identical
pickles. That's what I think of "immutable" as meaning, and obviously
isn't the case with this L.
The real point I was trying to make is that there are two definitions of
"constant" that we care about: immutability and resistance to name re-
binding. (Unbindability?) Python already gives us all the tools we need
for immutability (modulo the usual disclaimers about "we're all adult
here", "private attributes are private by convention", etc.). What Python
doesn't have is any way of prohibiting name rebinding short of creating a
new reserved word like None.

I think I'd also want to be able to make instance attributes
un-rebindable. E.g.

class Foo(object):
@const
def wibble(self, a): ...

it shouldn't be possible to redefine x.wibble if x is a Foo. That
should allow for compile-time binding in a lot of practical code.
 
B

BartC

Steven D'Aprano said:
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.
 
A

alex23

BartC said:
Another example:

pi=3.141592654

print ("pi is:",pi)

pi=42

print ("pi is now:",pi)

which is clearly undesirable.

Maybe not if you're the state of Indiana :)
 
J

John Ladasky

    All functions in Python can be replaced dynamically. While they're
running. From another thread.  Really.

Indeed, and I find this feature VERY useful when coding. Two places
I've used it are:

1) in GUI coding (say, when I have a panel of buttons, and each one
has its own associated function in my code -- but that function's name
is not part of the object definition itself) and

2) when profiling code, and I'm trying out various versions of a
function. For example, I'll write:

for func in (func1, func2, func3):
# do something with func()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top