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

Christophe chris.cavalaria at free.fr
Wed May 3 06:06:03 EDT 2006


Fredrik Lundh a écrit :
> tsaar2003 at yahoo.com wrote:
> 
> 
>>For example:
>>
>>
>>>>>A = []  # let's declare a "constant" here
>>>>>b = A   # and let's assign the constant here
>>>>>b.append('1') # OOPS!
>>>>>c = A
>>>>>print A
>>
>>['1']
>>
>>>>>print b
>>
>>['1']
>>
>>>>>print c
>>
>>['1']
>>
>>As you can see, the "constant" A can be modified this easily. But if
>>there were an intuitive mechanism to declare a symbol to be immutable,
>>then there won't be this problem.
> 
> 
> why are you using mutable objects as constants?
> 
> (insert obligatory "it hurts when I do this" joke here)
> 
> btw, given a hypothetical "object that symbol points to is immutable" syntax,
> what would you expect this to do ?
> 
>     >>> constant A = []
> 
>     >>> b = [A]
> 
>     >>> # much later
>     >>> b[0].append('1')

That's easy, since A is a symbolic constant know at compile time, and 
since it's a known mutable objet, the code once compiled will be 
equivalent to:

 >>> b = [[]]

 >>> # much later
 >>> b|0].append('1')



More information about the Python-list mailing list