[Tutor] function return values

Cameron Simpson cs at cskk.id.au
Fri Dec 3 15:46:57 EST 2021


On 03Dec2021 18:14, Peter Otten <__peter__ at web.de> wrote:
>On 03/12/2021 08:04, Cameron Simpson wrote:
>>_However_, the == operator binds more tightly that the , operator. So
>>this:
>>
>>     if item, row, column_list == None, None, None:
>>
>>is actually a 5-tuple:
>>
>>     item
>>     row
>>     column_list==None
>>     None
>>     None
>>
>>Being a non-empty tuple, it is always true. Not what you wanted to test.
>
>I believed you, but something made me feed this to the interpreter:
>
>>>> 1, 2 == 3, 4
>(1, False, 4)
>>>> if 1, 2 == 3, 4: print("equal")
>SyntaxError: invalid syntax
>
>So the unsuspecting writer gets one more chance to put the parens into 
>the right places ;)

Fascinating. I did not expect this!

On consideration, this will only confuse people coming from C, who might 
think "oh I need brackets around the condition" and write:

    >>> if (1, 2 == 3, 4):
    ...   print(1)
    ...
    1

In Python 3.8 I get this:

    >>> if 1, 2 == 3, 4:
      File "<stdin>", line 1
          if 1, 2 == 3, 4:
              ^
      SyntaxError: invalid syntax

which does suggest the commas as the issue, not a missing opening 
bracket, but still...

Thanks,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list