5 ms·
:= wasn't needed. = is just fine.
by lcnmrn 7y ago
:= wasn't needed. = is just fine.
- postpawl 7y agoUsually you don’t want to assign things in IF statements. Most of the time “if (a = b)” is intended to be “if (a == b)”, hence the need for the walrus operator.
- WillPostForFood 7y agoAte there any good reasons to assign things in an if statement?
- Sean1708 7y agoI have to work with stacks a lot and doing while (item := stack.pop()) is not None: # ... is a lot harder to get wrong than item = stack.pop() while item is not None: # ... item = stack.pop() Really wish it was scoped though... Edit: Just realised you were asking for an if-statement... but I think this is still a valid point.
- wffurr 7y agoScope the assigned variable to the if statement.
- mc3 7y agoIf so, it might be a code smell to consider defining a separate function.
- rs23296008n1 7y agoSo far, all the suggested usages of := have a code smell about them.
- archgoon 7y agothat's not how the walrus operator works though. if x := 3: print("x is now: ", x) print("x isn't scoped to the if:",x)
- Thorrez 7y agoThe post's example with reductor seems like a good reason to me.
- thaumasiotes 7y agoDon't think of the walrus as being an assignment. You can conceptualize it that way, but you shouldn't; that thought will be unhelpful in producing and reading code. Assignment is still the normal =, with a variable on the left and the value you want to put in it on the right. The walrus is for giving names to values you were computing anyway. Instead of a storage bin and the value you want to store there, the walrus involves a value and the name you want to refer to it by. Conceptually, the value is where you begin.
- willtim 7y agoThat is really really confusing given that there is lots of prior art for = meaning variable binding and := assignment (Algol, Pascal etc). Using = for assignment was a bad decision and it's coming back to bite. EDIT: Based the examples given, the Python := operator doesn't appear to behave as a let binding. The scoping suggests it is another form of assignment.
- thaumasiotes 7y agoIt is another form of assignment if you're implementing a Python compiler. The intended use is as a tool for naming values.
- deleted 7y ago[deleted]
- willtim 7y agoI'm trying to understand the semantics not the implementation. The semantics are clearly not "let x = .. in .." as anyone familiar with expression let-bound variables would expect. I wish I could avoid Python but sadly it is taking over the world. It's even taught at schools now and my kids will be learning it as their first programming language.
- throwawayhhakdl 7y ago“Need” feels like a strong choice of words here. A strong point of python is its readable by people who aren’t familiar with it. := has no intuitive meaning
- squiggleblaz 7y agoBut the intuitive meaning of = and the actual Python meaning of = are completely different. Which is the entire point here. Indeed, there's no intuitive of "the box that is referred to by the name on the left shall now contain the value that is the result of the computation on the right" since serious humans have had no need to express the concept. Most people who code in Python have no idea what they're doing. It's all magical incantations that they fiddle with until it gives the output they want. That's why python prohibits if x = some_computation() since if it permitted it, everyone would be all wtf. But the number of people who can gather that "sequence of symbols taken as a unit" has some arbitrary new meaning almost certainly significantly exceeds the number of people who can gather that "symbol traditionally used for signifying equality" has some arbitrary new meaning. And, at the very least, if you see some incredible incomprehensible sequence of symbols you can ask what they mean. It is, however, necessary that you have not unknowingly misunderstood what the symbols mean before you can seek clarification, since no-one ever asks for clarification for something they fully but incorrectly understand.
- robomartin 7y agoThat's it. Symbols, notation, have power. Most programmers don't understand this because they have never been exposed to the idea. It is only natural that people reject it violently when proposed.
- rs23296008n1 7y ago:= code examples all seem less clearer. Too easily abused.