2 ms·
In this case `query_params` works well, but it's sometimes hard to find descriptive and reasonably concise names for the intermediate value. In those cases, the
by skitter 4y ago
In this case `query_params` works well, but it's sometimes hard to find descriptive and reasonably concise names for the intermediate value. In those cases, the ideal would be using only postfix chaining, so that you can read it by only keeping the intermediate value and the next operation in mind:
s.split('?')[1]
.split('&')[-3:]
.map(lambda x: x.split('=')[1])
Unfortunately, that's not how Pythons map(), len() and such were designed.
- gilch 4y agoIdiomatic Python wouldn't use a map here, but a generator expression: (x.split('=')[1] for x in s.split('?')[1].split('&')[-3:]) Removing the lambda cuts down on the noise considerably. And honestly, with this many splits with fixed indexes, I'd probably use a regex. Now there's a dense language for you.
- chii 4y agoi don't really see why or how a generator expression is any easier to read than a chained call like the OP's example. In fact, for people unfamiliar with python, this expression is even more strange - you read expression starting from the middle (the _in_ ... part), and then return to the beginning. It makes your eye dart forward and backwards on the text.