Possible constant assignment operators ":=" and "::=" for Python

C

Christophe

Fredrik Lundh a écrit :
Christophe wrote:

I think you've made a mistake in your example.
constant A = []
def foo(var):
... var.append('1')
... print var
...
b = A
foo(b)
foo(b)
and this ?

constant A = []
print A is A

Obviously, False.


why obviously ? why shouldn't a constant be constant ?

</F>

Because the name A is bound by the compiler to mean construct a new
object since [] is mutable.

Same reason why :
>>> def A(): return []
>>> print A() is A()
False

It gives btw one possible implementation ;)
 
P

Piet van Oostrum

T> As you can see, the "constant" A can be modified this easily. But if
T> there were an intuitive mechanism to declare a symbol to be immutable,
T> then there won't be this problem.

Mutability is not a property of symbols but of values. So it doesn't make
sense to declare an identifier to be immutable. And mutability is tied to
the object's type, not to individual instances.

What you want can only be obtained if Python would have an immutable
variant for each type or would add an immutable attribute to each object.
 
E

Edward Elliott

Piet said:
Mutability is not a property of symbols but of values. So it doesn't make
sense to declare an identifier to be immutable. And mutability is tied to
the object's type, not to individual instances.

I think he meant immutable binding, not immutable symbol. So
rebinding/overshadowing a "constant" A would raise an error, but mutating
the underlying object A refers to would not (unless it too were immutable).
As far objects themselves, adding an ability to make any object immutable
regardless of type is exactly what he suggests.
 
P

Piet van Oostrum

Edward Elliott said:
T> As you can see, the "constant" A can be modified this easily. But if
T> there were an intuitive mechanism to declare a symbol to be immutable,
T> then there won't be this problem.
EE> I think he meant immutable binding, not immutable symbol. So
EE> rebinding/overshadowing a "constant" A would raise an error, but
EE> mutating the underlying object A refers to would not (unless it too
EE> were immutable).

The way I understood it was that he meant both.
 
S

Steve Holden

Piet said:
The way I understood it was that he meant both.

The way I understood it was that he didn't understand it. But then he
was gracious enough to admit that under questioning.

regards
Steve
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top