Questions tagged [python-assignment-expression]

Assignment expressions (a := b) are a new syntactic structure 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.

14 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…
27
votes
1 answer

With assignment expressions in Python 3.8, why do we need to use `as` in `with`?

Now that PEP 572 has been accepted, Python 3.8 is destined to have assignment expressions, so we can use an assignment expression in with, i.e. with (f := open('file.txt')): for l in f: print(f) instead of with open('file.txt') as f: …
Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
27
votes
4 answers

Is it possible to add a where clause with list comprehension?

Consider the following list comprehension [ (x,f(x)) for x in iterable if f(x) ] This filters the iterable based a condition f and returns the pairs of x,f(x). The problem with this approach is f(x) is calculated twice. It would be great if we…
11
votes
1 answer

Can assignment expressions be enabled in Python 3.7 using __future__?

Python 3.8 introduces assignment expressions, described in PEP 572. Is there a way to test this new feature in Python 3.7.x? In the past, new language features have been backported to earlier Python versions using __future__ imports. Is there a…
6
votes
1 answer

How to Avoid Leaking of Python Assignment Expressions in Comprehensions

In the Effective Python book, the author recommends using assignment expressions to avoid redundancy in comprehensions, for example: def fun(i): return 2 * i result = {x: y for x in [0, 1, 2, 3] if (y := fun(x)) > 3} instead of result = {x:…
Kilian Batzner
  • 5,420
  • 2
  • 32
  • 44
6
votes
3 answers

How to rewrite this simple loop using assignment expressions introduced in Python 3.8 alpha?

It appears to me that it is not that straight forward to interchange classic while loops with assignment-expressions-loops keeping the code looking great. Consider example1: >>> a = 0 >>> while (a := a+1) < 10: ... print(a) ...…
alec_djinn
  • 7,288
  • 8
  • 29
  • 58
3
votes
3 answers

Python 3.8 assignment expression in a list comprehension

I'm trying to use the new assignment expression for the first time and could use some help. Given three lines of log outputs: sin = """Writing 93 records to /data/newstates-900.03-07_07/top100.newstates-900.03-07_07/Russia.seirdc.March6-900.12.csv…
StephenBoesch
  • 46,509
  • 64
  • 237
  • 432
3
votes
1 answer

How can an assignment statement "x = y := f(x)" be done when using assignment expressions in Python?

I read in Twitter: #Python news: Guido accepted PEP 572. Python now has assignment expressions. if (match := (pattern.search) pattern.search(data)) is not None: print((match.group) mo.group(1)) filtered_data = [y for x in data if (y :=…
fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
2
votes
3 answers

Can assignment expression create Fibonacci series using list comprehension?

With assignment expression, I thought I could try list comprehension to create Fibonacci. I first initialize a Fibonacci list of 5 elements f = [1,2,3,4,5] with the first two values being the seeds. The test run below shows the assignment expression…
Leon Chang
  • 335
  • 2
  • 6
2
votes
1 answer

Understanding the reason for the new python := operator

This is kind of a meta programming question: I'd like to understand why python developers introduced yet another operator with the new :=. I know what it's for. I would, however, like to know, why the developers chose a new symbol instead of…
0
votes
2 answers

assignment expressions with conditional expression

The following snippet def expensive_function(x): return x x = 10.5 (int(y) if y.is_integer() else y := expensive_function(x)) raises SyntaxError: cannot use assignment expressions with conditional expression Can assignment expressions not be…
Alexander McFarlane
  • 8,800
  • 7
  • 47
  • 91
0
votes
0 answers

Assignment expression without direct use

I'd like to introduce assignment expressions in my code where possible but I've stumbled upon a very simple case I didn't find an example for: Given a very common example [(y := f(x), x/y) for x in some_iterable] I wonder how to not use y directly,…
frans
  • 6,905
  • 8
  • 40
  • 95
0
votes
1 answer

Multiple conditions for "walrus operator" assignment

I'd like to know if it's possible to use the "walrus operator" to assign the value based on some condition as well as existing. For example, assign the string to post_url if that string contains some substring: if post_url := data.get("Post url")…
Shaun Barney
  • 596
  • 5
  • 20
-2
votes
3 answers

Using Python 3.8 assignment expressions as a let expression?

My intended behavior is: >>> x = 0 >>> with (x := 1): print(x) 1 >>> print(x) 0 However I get the expected AttributeError: __enter__ error. Is there an easy way to achieve this, or something similar that lets me compensate for not having Lisp-style…
Atonal
  • 430
  • 3
  • 12