[Python-ideas] Two small functional-style-related improvements
zuo at chopin.edu.pl
Sun Mar 27 13:53:26 CEST 2011
Ben Finney dixit (2011-03-27, 14:14):
> Jan Kaliszewski <zuo at chopin.edu.pl> writes:
> > IMHO it'd be nice...
> > 1. ...to add:
> > * operator.is_none -- equivalent to (lambda x: x is None))
> > * operator.is_not_none -- equivalent tolambda x: x is not None))
> Why so specific?
None is quite specific (and widely used in different contexts) and is
None/is not None tests are very common.
> What's wrong with ‘operator.is_(x, None)’ and
> ‘operator.is_not(x, None)’? Those both work today.
But cannot be used quickly with all(), any(), filter(), takewhile(),
dropwhile() etc. without ugly lambda or playing with partial().
Which one of the following do you prefer?
* filter((lambda x: x is None), iterable)
* filter(functools.partial(operator.is_, None), iterable)
* filter(None, (x is None for x in iterable))
* filter(operator.is_none, iterable)
> > 2. ...to add:
> > * operator.anti_caller (or e.g. functools.negator?) -- equivalent to:
> > def anti_caller(func):
> > def call_and_negate(*args, **kwargs):
> > return not func(*args, **kwargs)
> > return call_and_negate
> This one seems overkill for the standard library.
But itertools.filterfalse() was added -- which is counterpart for
filter(). Why not to cover also all(), any(), takewhile(), dropwhile()
etc. with only one additional function?
More information about the Python-ideas