walrus-operator Questions
2
Solved
Looking at Python-Dev and StackOverflow, Python's ternary operator equivalent is:
a if condition else b
Looking at PEP-572 and StackOverflow, I understand what Walrus operator is:
:=
Now I'm tryi...
Schenk asked 6/8, 2020 at 18:16
2
Solved
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 fiv...
Pheasant asked 13/1, 2022 at 15:3
2
Solved
I wanted to avoid double evaluation of a mean in a dict comprehension, and I tried using the walrus operator:
>>> dic = {"A": [45,58,75], "B": [55,82,80,92], "C&qu...
Elah asked 5/10, 2021 at 14:30
2
Solved
I'm trying to understand the walrus assignment operator.
Classic while loop breaks when condition is reassigned to False within the loop.
x = True
while x:
print('hello')
x = False
Why doesn't t...
Concentrate asked 30/12, 2020 at 22:51
1
Solved
I have the following expression:
>>> a = 3
>>> b = 2
>>> a == (a := b)
False
Now, a == 2 after the operation, as expected. And the result is what I would want, i.e., com...
Pm asked 13/9, 2021 at 18:55
1
Solved
This is the code I write right now:
a = 1
if (a := a + 1) == 2:
print(a)
I am wondering if something like this exists:
a = 1
if (a +:= 1) == 2:
print(a)
Impasse asked 24/7, 2021 at 4:19
2
I am trying to type hint a walrus operator expression, i.e.
while (var: int := some_func()): ...
How can I do this?
Acerbate asked 3/6, 2021 at 20:6
1
Solved
I have a simple function that should output a prefix based on a pattern or None if it does not match. Trying to do a walrus it does not seem to work. Any idea?
import re
def get_prefix(name):
if ...
Nofretete asked 12/5, 2021 at 10:35
1
Solved
Why can't I use the walrus operator := to assign to an attribute? It works when assigning to a local variable:
my_eyes = ["left", "right"]
if saved_eye := my_eyes.index("le...
Hawkeyed asked 26/4, 2021 at 2:38
2
Solved
I just learned that the new walrus operator (:=) can't be used to set instance attributes, it's supposedly invalid syntax (raises a SyntaxError).
Why is this? (And can you provide a link to officia...
Swirl asked 24/9, 2020 at 22:33
1
Solved
PEP 572 introduces the assignement operator ("walrus operator").
The following code works, and outputs empty
def say_empty():
return ''
if a := say_empty():
print("not empty"...
Subordinate asked 20/2, 2021 at 7:47
1
© 2022 - 2024 — McMap. All rights reserved.