What I don't get is all the fuss they had to go through to implement this, since I remember at the time, python devs were pretty polarized by this feature (as in, let's implement vs let's not).
It sounds such a minor improvement to even spend more than 5 minutes thinking about it. Unless I'm missing something crucial.
it is nice to use inside dictionary values, this is an example inspired on some code i wrote a few hours ago with the walrus op:
blablabla.create(map(lambda record: {
...other key value pairs...
'task_id': (task := get_task(record)).id,
'project_id': task.project.id,
...other key value pairs...
}, records))
Again, this is not necessary perse, it is syntax sugar, and with one thing it is a bit senseless since you only save one line, but when you have a case like the above but with multiple values and inside a list comprehension or a map, it becomes very useful to make the code a little bit easier to read and easier to refactor in the future.
I use them from time to time, it looks nicer than having a wall of lines of variables that you preassign before a large if else chain to use in the
comparation and in the code inside.
And as the example has shown, it can also be used outside of ifs, you can use it inside a map or a comprehension to set values that depend on a value that you are computing inside the map/comprehension
View all comments
Show parent comment
2
u/JestemStefan May 29 '22 edited May 29 '22
Instead of
You can do: