Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
Why aren't augmented assignment expressions allowed?

I was recently reading over PEP 572 on assignment expressions and stumbled upon an interesting use case:

# Compute partial sums in a list comprehension
total = 0
partial_sums = [total := total + v for v in values]
print("Total:", total)

I began exploring the snippet on my own and soon discovered that :+= wasn't valid Python syntax.

# Compute partial sums in a list comprehension
total = 0
partial_sums = [total :+= v for v in values]
print("Total:", total)

I suspect there may be some underlying reason in how := is implemented that wisely precludes :+=, but I'm not sure what it could be. If someone wiser in the ways of Python knows why :+= is unfeasible or impractical or otherwise unimplemented, please share your understanding.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  1. My understanding is that a major part of comprehension efficiency is that the calculations for each element in the original iterable can be performed in parallel. If you're calculating a running total, that has to be performed in series.

  2. We already have sum(values), so what does this add in this use case?

  3. If you want something for more general use cases, functools has reduce.

over 4 years ago · Santiago Trujillo Report

0

The itertools module provides a lot of the mechanisms to achieve the same result without bloating the language:

partial_sums = list(accumulate(values))
total        = partial_sums[-1] 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!