I'm not much of a Python guy, but that chained comparison operator is sweet! Sure, it's just syntax sugar, but it saves a lot of keystrokes, especially if the variable name is long. Is Python the only language with this feature?
Python idioms I wish I'd learned earlier
21–30 of 174 posts
Re: Python idioms I wish I'd learned earlier
#22 opt = {0: do_a,
1: do_b,
3: do_b,
4: do_c}
opt[option]()Re: Python idioms I wish I'd learned earlier
#23Re: Python idioms I wish I'd learned earlier
#24> There is a solution: parentheses without commas. I don't know why this works, but I'm glad it does. It's worth mentioning that this is a somewhat controversial practice. Guido has even discussed removing C-style string literal concatenation: http://lwn.net/Articles/551438/ You may wish to consult your project's style guide and linter settings before using it.
I think in most cases it's better to use triple quotes. And if the content of these variables isn't exclusively shown in the shell, you should use translation files anyway.
Re: Python idioms I wish I'd learned earlier
#25This is something I do instead of writing a long if-else: opt = {0: do_a, 1: do_b, 3: do_b, 4: do_c} opt[option]()
Re: Python idioms I wish I'd learned earlier
#26Earlier quoted context omitted.
That's cute, but the result of a bad design decision. Python overloads "+" as concatenate for strings. This also applies to lists. So [1,2,3] + [4,5,6] yields [1,2,3,4,5,6] This is cute, but not what you want for numerical work. Then, viewing multiplication as repeated addition, Python gives us [1,2,3]*4 yields [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] This is rarely what was wanted. Then there's numpy, which has its own…
"This is rarely what was wanted." I don't know what else you would have expected...
Re: Python idioms I wish I'd learned earlier
#27Earlier quoted context omitted.
Common Lisp (and other dialects): ( Also: (lcm a b c d ...) ;; lowest common multiple (+) -> 0 (+ a) -> a (+ a b) -> a + b (+ a b c) -> (a + b) + c (*) -> 1 (* a) -> a (* a b) -> a * b (* a b c) -> (a * b) * c Is it just syntactic sugar? ( (and ( isn't the same as ( By the way, this could be turned into a short-circuiting operator: more semantic variation. Suppose < is allowed to control evaluation. Then an expressio…
But can any lisp dialect do: a = c ?
Re: Python idioms I wish I'd learned earlier
#28This is something I do instead of writing a long if-else: opt = {0: do_a, 1: do_b, 3: do_b, 4: do_c} opt[option]()
Do you consider that to be idiomatic? I've been out of touch with the a Python community for a few years, but back then I wouldn't have considered that remotely idiomatic, and if I was on a team writing software, I would have argued that we shouldn't be writing code like that.
And it's something I do on occasion. For example:
https://github.com/ubernostrum/webcolors/blob/master/webcolo...
Re: Python idioms I wish I'd learned earlier
#29This is something I do instead of writing a long if-else: opt = {0: do_a, 1: do_b, 3: do_b, 4: do_c} opt[option]()
Do you consider that to be idiomatic? I've been out of touch with the a Python community for a few years, but back then I wouldn't have considered that remotely idiomatic, and if I was on a team writing software, I would have argued that we shouldn't be writing code like that.
Re: Python idioms I wish I'd learned earlier
#30This is something I do instead of writing a long if-else: opt = {0: do_a, 1: do_b, 3: do_b, 4: do_c} opt[option]()
Do you consider that to be idiomatic? I've been out of touch with the a Python community for a few years, but back then I wouldn't have considered that remotely idiomatic, and if I was on a team writing software, I would have argued that we shouldn't be writing code like that.
I'd love to hear why you think it's not ideal.