5 ms·
Trailing commas are really easy to miss. When reading this line of code, I did not notice it immediately; I originally assumed that varname was being assigned a
by tikhonj 14y ago
Trailing commas are really easy to miss. When reading this line of code, I did not notice it immediately; I originally assumed that varname was being assigned a list.
This sort of code would be very confusing when I'm just quickly reading through a procedure trying to find the potential bug.
- vog 14y agoIndeed. I prefer the following variant which is more explicit and thus (IMHO) more in the spirit of Python: (varname,) = [x for x in l if predicate_with_single_truth_value(x)]
- almost 14y agoAgreed. It could be written much more clearly in my opinion like this: [varname] = [x for x in l if predicate_with_single_truth_value(x)]
- Estragon 14y agoGood idea!
- euccastro 14y agoI had to check that on the REPL. I'm surprised that even works and I can't think of a good reason why should list syntax be allowed as a lvalue, in addition to tuple syntax.
- Estragon 14y agoIt's called destructuring assignment. It's been around for a while. http://dunsmor.com/lisp/onlisp/onlisp_22.html http://dunsmor.com/lisp/onlisp/onlisp_22.html
- lucian1900 14y agoYou could also use the ,= operator, of course: varname ,= [...]
- andreasvc 14y agoIt appears to me that there is actually no such operator in Python; cf. http://docs.python.org/reference/simple_stmts.html#augmented-assignment-statements http://docs.python.org/reference/simple_stmts.html#augmented... Superficially it looks like an operator, but I suspect that's merely because of whitespace freedom; i.e., a, = [0] is equivalent to a,=[0] and a ,= [0].
- jgeralnik 14y agoI assume it was a joke; a pop culture reference to this stack overflow question: http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator http://stackoverflow.com/questions/1642028/what-is-the-name-...
- lucian1900 14y agoSure it is! And in Python 3 there's the ,_*= operator, similar to lisp's car: varname ,_*= [1, 2, 3] # varname == 1
- andreasvc 14y agoHN isn't the place for facetiousness.
- lucian1900 14y agoApparently it isn't the place for humour either.