Is there a correct way to have two walrus operators in 1 if statement?
if (three:= i%3==0) and (five:= i%5 ==0):
arr.append("FizzBuzz")
elif three:
arr.append("Fizz")
elif five:
arr.append("Buzz")
else:
arr.append(str(i-1))
This example works for three but five will be "not defined".
CodePudding user response:
The logical operator and evaluates its second operand only conditionally. There is no correct way to have a conditional assignment that is unconditionally needed.
Instead use the "binary" operator &, which evaluates its second operand unconditionally.
arr = []
for i in range(1, 25):
# v force evaluation of both operands
if (three := i % 3 == 0) & (five := i % 5 == 0):
arr.append("FizzBuzz")
elif three:
arr.append("Fizz")
elif five:
arr.append("Buzz")
else:
arr.append(str(i))
print(arr)
# ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', ...]
Correspondingly, one can use | as an unconditional variant of or. In addition, the "xor" operator ^ has no equivalent with conditional evaluation at all.
Notably, the binary operators evaluate booleans as purely boolean - for example, False | True is True not 1 – but may work differently for other types. To evaluate arbitrary values such as lists in a boolean context with binary operators, convert them to bool after assignment:
# |~~~ force list to boolean ~~| | force evaluation of both operands
# v v~ walrus-assign list ~vv v
if bool(lines := list(some_file)) & ((today := datetime.today()) == 0):
...
Since assignment expressions require parentheses for proper precedence, the common problem of different precedence between logical (and, or) and binary (&, |, ^) operators is irrelevant here.
CodePudding user response:
The issue you are having is that five is only assigned if three is True in this statement because of short circuiting:
if (three:= i%3==0) and (five:= i%5 ==0)
so five commonly is not assigned causing either a NameError or using a non-current value.
You can force a True value by forming a non-empty tuple with the walrus assignment inside of it then using three and five as you expect after that tuple.
It is no prettier than assigning three and five prior to the if but this works:
arr=[]
for i in range(1,26):
if (three:=i%3==0, five:=i%5==0) and three and five:
arr.append(f"{i} FizzBuzz")
elif three:
arr.append(f"{i} Fizz")
elif five:
arr.append(f"{i} Buzz")
else:
arr.append(f"{i}")
>>> arr
['1', '2', '3 Fizz', '4', '5 Buzz', '6 Fizz', '7', '8', '9 Fizz', '10 Buzz', '11', '12 Fizz', '13', '14', '15 FizzBuzz', '16', '17', '18 Fizz', '19', '20 Buzz', '21 Fizz', '22', '23', '24 Fizz', '25 Buzz']
Any non-empty tuple is True in Python. Forming it causes (three:=i%3==0, five:=i%5==0) to always be truthy and three and five to be assigned each time. Since that tuple is true, the rest of the expression has to be evaluated with the correct values of three and five.
BTW, speaking of tuples, a shorter way to do a FizzBuzz type challenge in Python:
fb={(True,True):"{} FizzBuzz",
(True,False):"{} Fizz",
(False,True):"{} Buzz",
(False,False):"{}"}
arr=[fb[(i%3==0,i%5==0)].format(i) for i in range(1,26)]
And if you are looking for something new this type of problem is a natural for Python 3.10 pattern matching:
arr=[]
for i in range(1,26):
s=f"{i}"
match (i%3==0,i%5==0):
case (True, (True | False) as oth):
s =" FizzBuzz" if oth else " Fizz"
case (False, True):
s =" Buzz"
arr.append(s)
