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
0
votes
2 answers

Can the walrus operator be used to avoid multiple function calls within a list comprehension?

Let's say I have a list of lists like this lol = [[1, 'e_r_i'], [2, 't_u_p']] and I want to apply a function to the string elements which returns several values from which I need only a subset (which ones differ per use-case). For illustration…
Cleb
  • 20,118
  • 16
  • 91
  • 131
0
votes
3 answers

Is there a way to do a while loop on a variable that's undefined? (How can I rewrite this, Python)

I want to do a while loop that runs until a user puts nothing in the input. This is what I have that currently works, but I want to remove the answer = None instantiation. def answer_as_ul(question, input_prefix='• '): print(question) …
Zack Plauché
  • 798
  • 5
  • 13
0
votes
1 answer

Walrus Operator: NameError: free variable 'it' referenced before assignment in enclosing scope

I want to create a list using the walrus operator like this: myvar = tr_list[8].css('td ::text').extract() item['myvar'] = [ it := re.sub(PATTERN, "", i).strip() for i in myvar if len(it) > 0 ] And I get this error: it :=…
Victor M Herasme Perez
  • 1,945
  • 2
  • 12
  • 20
0
votes
3 answers

Why can I not assign to a named expression (LHS walrus operator)?

Assigning to expressions (as opposed to names) is commonplace in Python. For example, this is perfectly valid syntax: my.object["with_some"].very_long["expression"] = func(my.object["with_some"].very_long["expression"],…
gerrit
  • 17,590
  • 12
  • 72
  • 135
0
votes
1 answer

Why doesn't the walrus operator pass keyword arguments?

Why doesn't the walrus operator pass the keyword argument figsize to matplotlib.pyplot.figure in this code? #TODO: visualize whether the index is a valid x_value fontsize=21 plt.figure(figsize:=(8,8)) plt.scatter(x_values_theory, y_values_theory,…
Tim Tyree
  • 9
  • 1
0
votes
1 answer

Use walrus operator when checking value

How can I check if a variable is equal to something, and set to a new variable in the child scope? For example: bar = 'foobar' my_slice = bar[:3] if my_slice == 'foo': print(my_slice) It seems like the new walrus operator here would be useful,…
arshbot
  • 4,458
  • 4
  • 25
  • 47
0
votes
1 answer

Walrus operator for filtering regex searches in list comprehension

I have a Python list of strings. I want to do the regex search on each element, filtering only those elements where I managed to capture the regex group. I think I can do the regex search only once using the walrus operator from Python 3.8. So far I…
qalis
  • 636
  • 4
  • 18
0
votes
0 answers

How to share packages of pip3.7 with pip3.8 - MacOs

I have python 3.7 installed at /usr/local/bin/python3.7 and I recently learned about a new operator called the walrus operator := that was introduced in python 3.8 here's how it works for those who are not familiar with it as myself 5 minutes…
sK500
  • 1
  • 2
  • 16
0
votes
1 answer

How can named expressions be interpreted in f-strings?

I am trying to use named expressions inside a f-string: print(f"{(a:=5 + 6) = }") Returns: (a:=5 + 6) = 11 But I am hoping for something like this: a = 11 Is that possible, by combining the walrus operator and f-strings (so that I don't have to…
-4
votes
1 answer

Bug in Walrus operator

I was working with the Walrus operator. I was getting the user to guess a number between 0 and 10. Their guess is then added to a list full of their guesses. But once they enter a 0, it just prints out the list. The problem I am having is that the…
1
2