Questions tagged [walrus-operator]

The "walrus operator" is an informal term for the assignment expression syntax (a := b) that were proposed in PEP 572 and introduced in Python 3.8

After acceptance of PEP 572, introduced assignment expressions, which are a way to assign to variables within an expression using the notation NAME := expr. Given the resemblance between := and an emoticon of eyes plus walrus tusks, the syntax is sometimes known as the "walrus operator", a term coined in the PEP.

25 questions
69
votes
3 answers

":=" syntax and assignment expressions: what and why?

PEP 572 introduces assignment expressions (colloquially known as the Walrus Operator), implemented for Python 3.8. This seems like a really substantial new feature since it will allow this form of assignment within comprehensions and lambda…
12
votes
2 answers

Why can't Python's walrus operator be used to set instance attributes?

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 official docs mentioning this?) I looked through PEP 572,…
5
votes
3 answers

How to check if the Enter key was pressed while using the Walrus operator in Python?

I'm trying to get input from a user using the Walrus operator :=, but if the user will only type in the Enter key as input, than the python script will terminate. How can I catch this error and make sure that the user hasn't only pressed the Enter…
MendelG
  • 4,464
  • 1
  • 5
  • 22
4
votes
1 answer

Why does using the walrus operator on a member variable raise a SyntaxError?

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("left"): print(saved_eye) # outputs >>> 0 But it is a syntax error if I…
4
votes
1 answer

Why does "if not a := say_empty()" raise a SyntaxError?

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") else: print("empty") I tried to negate the condition: def…
WoJ
  • 19,312
  • 30
  • 122
  • 230
4
votes
1 answer

Python Walrus Operator in While Loops

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 this work using the walrus operator? It ignores…
2
votes
1 answer

What is the correct syntax for Walrus operator with ternary operator?

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 trying to to combine the "walrus operator's assignment"…
2
votes
1 answer

operator or method in swift which work as walrus operator of python

walrus operator of python language ( := ) work:- assign the value & also return that value. language like swift at value assign it return nothing. how to implement walrus operator kind a thing in swift language ? I think it done by make function,…
1
vote
1 answer

Using a walrus operator in if statement does not work

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 m := re.match(f'^.+(\d\d)-(\d\d)-(\d\d\d\d)$',…
Bruno Vermeulen
  • 1,862
  • 2
  • 9
  • 18
1
vote
2 answers

Extracting value from list created with conditional string in list comprehension

What I have: series = ['foo', 'bar', 'baz', 'foo', 'baz', 'foo' ] column = [1, 2, -3, -4, 5, -6] list = [column[function(x)].count() for x in series] list: foo = 3 bar = 1 baz = 2 Works fine, each instance in series is counted. Want only…
1
vote
1 answer

Strange Behavior Python 3.8 Walrus Operator: chained inequalities

The following code: a,b=1,2 print((x:=a)<2<(z:=b) or z>1>x) print((x:=a)<1<(y:=b) or y>1>x) gives the following output: False Traceback (most recent call last): File "C:/Users/phili/PycharmProjects/ML 1/CodingBat exercises.py", line 56, in…
Porky623
  • 13
  • 2
1
vote
1 answer

Alternatives to using the walrus operator := inside a nested comprehension

Say, for the sake of demonstration, I have to take a list of "input points", and output the three pairs of numbers starting at those points: 1 -> [(1, 2), (3, 4), (5, 6)] 12 -> [(12, 13), (14, 15), (16, 17)] ... I could do this, in an extended for…
Green Cloak Guy
  • 18,876
  • 3
  • 21
  • 38
1
vote
1 answer

Python: Is there a Walrus Operator for slices of an object?

My question is, in all of the walrus examples, they use the whole object for the boolean, e.g. if (x := len(s)) > 5: print(x) converts x = len(s) if x > 5: print(x) Is there a way to do it for slices of x, if x were a string? e.g. x[-1] !=…
stackz
  • 54
  • 6
1
vote
3 answers

Odd Syntax (Walrus operator in inheritance)

I was looking in python's grammar and was that you could use the walrus operator in inheritance! Not believing it, I tried it out: class foo: pass class bar(foobar := foo): def x(self): print("it works!") b = bar() b.x() This does not…
xilpex
  • 2,716
  • 2
  • 10
  • 38
0
votes
0 answers

What is the reason the Python Walrus Operator only works with variable names?

Valid code: x, y, z = 1, 2, 3 print(x := x + y) print(x, y, z) Invalid code: arr = [1, 2, 3] print(arr[0] := arr[0] + arr[1]) print(arr) Error: SyntaxError: cannot use assignment expressions with subscript Another example in which it would be…
Axoren
  • 522
  • 1
  • 6
  • 22
1
2