[Python-ideas] Two small functional-style-related improvements
Terry Reedy
tjreedy at udel.edu
Sun Mar 27 21:12:53 CEST 2011
On 3/27/2011 10:38 AM, Paul Moore wrote:
> 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...
Me neither, not without more motivation than list reading.
> 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))).
Ditto. And once the function is abstracted and named, the detailed
implementation does not matter so much.
> -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).
--
Terry Jan Reedy
More information about the Python-ideas
mailing list