Do not promote `None` as the first argument to `filter` in documentation.

Chris Angelico rosuav at gmail.com
Tue Mar 6 10:37:01 EST 2018


On Wed, Mar 7, 2018 at 2:33 AM, Kirill Balunov <kirillbalunov at gmail.com> wrote:
>
>
> 2018-03-06 17:55 GMT+03:00 Chris Angelico <rosuav at gmail.com>:
>>
>> If the first argument is None, the identity function is assumed. That
>> is, all elements of the iterable that are false are removed; it is
>> equivalent to (item for item in iterable if item). It is approximately
>> equivalent to (but faster than) filter(bool, iterable).
>>
>> ChrisA
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> If you look in C source for `filter_next`
> https://github.com/python/cpython/blob/5d92647102fac9e116b98ab8bbc632eeed501c34/Python/bltinmodule.c#L593,
> there is a line:
>
> int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
>
> So the only difference between `filter(None, ls`) and `filter(bool, ls)` is
> LOAD_NAME vs LOAD_CONST and that `None` is checked before than `bool`.
>

Assuming that nobody's shadowed the name 'bool' anywhere, which has to
be checked for at run time. (Which is the job of LOAD_NAME.)

ChrisA



More information about the Python-list mailing list