[issue43055] Inconsistent behaviour when using walrus operator with 'and'/'or'

2021-01-28 Thread Eric V. Smith


Eric V. Smith  added the comment:

I'm going to close this. The short circuit evaluation of and/or expressions is 
already documented, and this behavior follows directly from that.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43055] Inconsistent behaviour when using walrus operator with 'and'/'or'

2021-01-28 Thread Eric V. Smith


Eric V. Smith  added the comment:

I think this is pretty clearly the intended semantics. In your first example, 
what do you propose "print(x)" would produce? Consider:

if False and (x:=input()) != '':
  pass
else:
  print(x)

We cannot change the semantics to now call input(), or any function with side 
effects, just to do the assignment to x.

--
nosy: +eric.smith

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43055] Inconsistent behaviour when using walrus operator with 'and'/'or'

2021-01-28 Thread Aman Anifer


New submission from Aman Anifer :

Using the walrus operator (:=) alongside 'and'/'or' shows inconsistent 
behaviour which changes with the order of given conditions. 

For example:
if(False and (x := 0)<1):
print("Yes")
else:
print(x)

Gives the error that 'NameError: name 'x' is not defined'
Whereas if the walrus operator is used first, like:
if((x := 0)>1 and False):
print("Yes")
else:
print(x)

Prints the value 0 without any error. This behaviour is the same when using 
'or'. For example:

if(True or (y:=0)<1):
print(y)
else:
print("No")

Gives the same error but this:
if((y:=0)<1 or True):
print(y)
else:
print("No")

Prints the value 0.
I am guessing that this is because 'and' doesn't check the second operand if 
the first operand is False, also 'if' doesn't check the second operand if the 
first operand is True. I don't know if this is an intended behaviour.

Thanks

--
components: Interpreter Core
messages: 385853
nosy: FuturisticGoo
priority: normal
severity: normal
status: open
title: Inconsistent behaviour when using walrus operator with 'and'/'or'
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com