9897 shaares
250 private links
250 private links
La PEP 572 : Assignment Expressions is useful in the following examples :
-
avoiding inefficient comprehensions
foo = [y := f(x), y*y, y*y*y]
-
unnecessary variables in scope
for x in data: result = f(x) if result: foo.append(result) # Turned into results = [ result for x in data if (result := f(x)) ]
-
processing streams in chunks
while chunk := file.read(8192): process(chunk)
-
Avoiding unecessary lines
group = match.group(1) if (match := re.match(data)) else None
Avec ces arguments, l'opérateur semble être une bonne amélioration pour le langage