3 ms·
Finally! I've long wanted to do for server in (server1, server2, *other_servers): Instead of for server in [server1, server2] + other_servers
by mikelward 11y ago
Finally!
I've long wanted to do
for server in (server1, server2, *other_servers):
Instead of
for server in [server1, server2] + other_servers
- rectangletangle 11y agoI've been wanting this feature for a while as a clean way to combine the results from several queries into a composite object. def combined(*dicts): return {k: v for dict_ in dicts for k, v in dict_.items()} dicts = [({'a': 'a', 'b': 'b'}, {'b': 'bb'}),] assert [{'a': 'a', 'b': 'bb'}] == [combined(a, b) for a, b in dicts]
- Too 11y agoCan't tell if you are sarcastic or not but to me the second line is much more readable.
- agumonkey 11y agoThat's mostly subjective, (a,b, °c) is a tuple of a, b and the rest being the sequence c. After reading a lot of lisp and ml, it's a bit less of a burden than [a,b] + l, in the sense that [a,b] is declarative mixed with an operation, while (a,b, °c) is one declarative. You might say `°` is an operation too, but it lives at the metalevel, and it's still enclosed in the (tuple-declaration).
- klibertp 11y agoNo, both lines are equally readable. A number of operations, placement of operators, names are all the same. The only difference is replacing "," with "]+", which is not any different for your eye* to read. What you mistake for readability is familiarity: after you read a word you need to associate a meaning with it. It takes longer to interpret the meaning of things you didn't see before while grabbing a meaning from a cache is nearly instantaneous. You understood this difference in comprehension speed as a readability difference. My main point is that it makes no sense to talk about readability without first getting used to the grammar and vocabulary of a language. In this case, it's unfamiliar feature in an otherwise normal environment, but it gets worse. There are people who believe they can judge the readability of entirely alien (to them) languages and it hurts those languages reputation pretty badly. Lisps are the prime example of this.
- deckiedan 11y agodef L(*vargs): return vargs tada. >>> stuff = ['c','d','e'] >>> L('a','b', *stuff) ('a', 'b', 'c', 'd', 'e') Just like that.
- indosauros 11y ago>>> L(*stuff, 'a', 'b') SyntaxError: only named arguments may follow *expression >>> L(*stuff, *stuff) SyntaxError: invalid syntax
- deckiedan 11y agoGood point... yes, having it language-level supported does add a lot of good stuff...