Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@kescherRant Because it's the only mathematically correct operator for assignment.
But it does not make too much sense if you do not use "=" for comparison.
Like everything in Python, this is something put on top of an existing stack of solutions. -
Interesting: https://docs.python.org/3/whatsnew/...
Apparently the idea is to replace double calls, or intermediary variables. It looks a little strange, but removing variable assignment does help reduce bugs. Which was a driver for comprehensions as well. I think this is an attempt to squash specific classes of bugs that can occur. Also it can help reduce code. Less things to maintain. -
haze2715yWhy?
E.g. for list comprehensions where you would need to repeat stuff in result and in if part.
Or in normal ifs, to check whether something exists, and assign it in the same line. -
@halfflat
Python is super edgy. Sometimes it cuts right to the chase, sometimes it cuts right into your developer soul. -
@tman540 imagine I need to calculate velocity and ALSO save both deltas. With Walrus it would be something like this:
```
x1 = 1
x2 = 2
t1 = 0
t2 = 3
v = (dx := (x2 - x1))/(dt := (t2 - t1))
print("v:", v) # v: 0.3...
print("DeltaX:", dx) # DeltaX: 1
print("DeltaT:", dt) # DeltaT: 3
```
Without it, it would become something like this:
```
x1 = 1
x2 = 2
t1 = 0
t2 = 3
dx = (x2 - x1)
dt = (t2 - t1)
v = dx/dt
print("v:", v) # v: 0.3...
print("DeltaX:", dx) # DeltaX: 1
print("DeltaT:", dt) # DeltaT: 3
```
It let's you assign values inside other expressions. -
fuckwit12185y@angrysnekguy I'd take this line or two if my colleagues can understand what I do there.
-
@gitcommit The only thing I don't understand is, why not just go the JS / PHP route and make assignment have a return value:
v = (dx = 2-1) / (dt = 4-2)
Just call it Python 4, it's not like they care about backwards compatibility 🤓 /troll -
Related Rants
Python 3.8 now has "the walrus operator"
:=
random
python
walrus