Supply condition in function call

Chris Angelico rosuav at gmail.com
Fri Mar 27 10:17:12 EDT 2015


On Sat, Mar 28, 2015 at 12:48 AM, Rustom Mody <rustompmody at gmail.com> wrote:
> Knows some C, not much else.
> Starts studying python.
> Good until a point.
> Then suddenly hit... map, filter, and the worst of all lambda.
> More he reads less he understands.
> Tries help... Gets the above.
>
> So which do you think helps him more python 2 or 3?

Python 3, because scaring someone off filter() might push him towards
comprehensions instead :)

No, seriously: I wouldn't expect anyone to be using help(filter) in
that way. Someone who knows C and not much Python is simply not going
to stumble upon filter, but will have a task/problem in mind. From
there, either the online documentation or a Q&A forum like this or
Stack Overflow will be the next port of call, not the help for one
particular function.

And frankly, if someone says "what you want is filter()" and doesn't
explain what it does, why it's useful, and why it's better than a list
comp/genexp, is doing a poor job of helping. The most common case of
filter can be trivially converted into a genexp:

filter(lambda x: some_expression, some_iterable)
(x for x in some_iterable if some_expression)

Unlike map(), filter() doesn't accept multiple iterables, so you don't
even need to worry about zip(). The only time you really need filter()
is when you're using a pre-existing function, and even then, a genexp
is often not much worse:

filter(int, list_of_strings)
(s for s in list_of_strings if int(s))

Instead of building up more and more complex nestings of map, filter,
etc, just go straight to a genexp (or a list comp, if you're about to
wrap it in list()) and add the complexity you need. Teaching a new
Python programmer about comprehensions is MUCH more useful than
teaching him/her about map and filter.

So, no. I don't think the help() difference between Py2 and Py3 is
going to be much of a problem to a new programmer who knows only C.
Maybe one who knows only LISP, but not one who knows C.

ChrisA



More information about the Python-list mailing list