Does Python need "const" ? (Re: PEP 218 Re: ANN: set-0.1 mod ule available)

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Mon May 20 13:25:25 EDT 2002


Gustavo Cordova <gcordova at hebmex.com> wrote:
>> [Huaiyu Zhu]
>> > # const reference to mutable object
>> > const a = "abc"
>> > #   forbids a = "xyz"
>> > #   allows b = a; a += "uvw"; which would set b to "abcuvw".
>> 
>> Presumably, you mean:
>> 
>> > #   allows b = a; b += "uvw"; which would set b to "abcuvw".
>> 

I meant what I wrote, but both are equally correct: a and b refer to the
same object.  However, you missed my paragraph immediately preceeding the
examples:

> To illustrate the idea, I'll give some examples, assuming that all things
> are mutable a priori without the 'const' keyword, which is not true today.
> Suppose we use prefix 'const' to refer to names and postfix 'const' to refer
> to objects, we could say

That is, in the examples, everything (including strings and numbers) is
assumed to be mutable unless used with the keyword 'const'.

>This seems quite contradictory:
>
>You intend "const" to forbid such things as "a = 'xyz'" once
>it's been binded to "a = 'abc'". But, you say it must permit
>"a += 'xyz'" so that a will end up as 'abcxyz'.

In these examples, which let me emphasize again is NOT today's python
behavior, "a = 'abc'" would bind a to a mutable string object whose content
is initialized to 'abc'.  It has a method += that could change its content
to 'abcxyz'.  In contrast, an "'abc' const" object would not allow such
change.

>But, strings are immutable objects, so a += operation will
>effectively bind "a" to an diffrent object, an operation
>more like:
>
>	a = "abc" + "xyz"
>
>How do you plan to attack the immutable object "problem"?

There would be two flavors of strings, mutable and immutable.  In-place
operators will not rebind mutable objects.  My understanding is that mutable
strings are already present in Python - but not exposed to the user space.
Presumably for mutable objects an "in-place assignment" operator would also
be handy:

const a = 'abc'
b = a
a = 'xyz'  # not allowed
a := 'xyz' # change object content in place
print b    # prints 'xyz'


To avoid further unnecessary confusion, let me clarify that

- this is not current Python behavior
- it is not a proposal for change
- it is only intended to facilitate the discussion about the role of
  mutability in many existing problems.
- the syntax is not used for any purpose other than this discussion.


Huaiyu



More information about the Python-list mailing list