assignment in control structure syntax (was Re: Curious assignment behaviour)

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Oct 10 18:00:13 EDT 2001


I have an alternative syntax that could satisfy both safety and simplicity.
It did not receive much notice the last time here, though.  The full
explanation is at http://www.geocities.com/huaiyu_zhu/python/ififif.txt

On Wed, 10 Oct 2001 09:47:11 +0200, Just van Rossum <just at letterror.com> wrote:
>Paul Rubin wrote:
>
>> Anyway, I can live with "while x:=get_next()" instead of x=get_next.
>> But I feel seriously cramped in Python when I have to say
>> 
>>   while 1:
>>      x=get_next()
>>      if not x: break
>>      whatever(x)
>> 
>> so I hope something is done about the issue.

while x = get_next(); x:
	whatever(x)

>
>I think this is the wrong example, as it can easily be written as a
>for loop. Iterators in 2.2 will make that even more natural. Now if
>you meant this:
>
>  m = someRegex.match(...)
>  if m:
>      ...
>
>vs.
>
>  if m := someRegex.match(...):
>      ...
>
>you may have a point...
>
>Just

if m = someRegex.match(...); m:
    ...

There are several other uses for this syntax.  For example,

if val = dict1[key1]; val:
    process1(val)
elif val = dict2[key2]; val:
    process2(val)
elif mylist += otherlist; len(mylist) > 4:
    process3(mylist)
...

These are arguably easier to understand than current Python syntax.
See the link above for more examples.

Is there much interest to turn this into a PEP?

Huaiyu



More information about the Python-list mailing list