[Python-ideas] Two small functional-style-related improvements

Paul Moore p.f.moore at gmail.com
Sun Mar 27 16:38:58 CEST 2011


On 27 March 2011 14:05, Jan Kaliszewski <zuo at chopin.edu.pl> wrote:
> I'd would like to be able to do:
>
>    assert any(dropwhile(negated(str.isalnum),
>                         takewhile(negated(str.isspace), my_names)))
>
> ...instead of:
>
>    assert any(dropwhile(lambda name: not name.isalnum(),
>                         takewhile(lambda name: not name.isspace(),
>                                   my_names)))

Do you honestly find *either* of those readable? I can't even work out
what they do well enough to try to come up with an alternative version
using generator expressions... If I needed an assertion like that, I'd
write a function with a name that explains what's going on, which does
the whole of that dropwhile-takewhile routine (probably using multiple
lines, with comments) and then use that as
assert(any(whatever_this_is(my_names))).

-1 on putting anything in the stdlib which encourages obfuscated code
like that. (Note: If that style suits you, then writing your own
functions in your codebase to support it is fine, I just don't see it
as something the stdlib should be doing).

Paul.



More information about the Python-ideas mailing list