This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Programming FAQ about "How do I apply a method to a sequence of objects?" should include the option of an explicit for-loop
Type: enhancement Stage:
Components: Documentation Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Dominik V., docs@python, mark.dickinson, rhettinger, veky
Priority: normal Keywords:

Created on 2020-04-20 21:06 by Dominik V., last changed 2022-04-11 14:59 by admin.

Messages (6)
msg366880 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2020-04-20 21:06
Right now the question is simply answered with:

> result = [obj.method() for obj in mylist]

However this is only appropriate if the result of the method is needed (i.e. if it's some kind of transformation).

There are many cases where it's not and the method is meant to update the object in place. Here it's better to use a for loop:

    for obj in mylist:
        obj.update()

Sometimes people use a one-way list comprehension hack because it saves one line:

    [obj.update() for obj in mylist]

However this is discouraged for multiple reasons (builds a superfluous list, obfuscates the actual intent, ...).

So I feel like the Programming FAQ should actively mention this scenario and promote the usage of a for loop here.
msg366885 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-20 21:17
The current answer seems reasonable to me.  It addresses the question that most people want to have answered.  Also, it isn't common to loop over methods that don't return values, because those typically do in-place mutations.
msg366901 - (view) Author: Vedran Čačić (veky) * Date: 2020-04-20 23:18
I must say I agree with Dominik here. Too many times my students write list comprehensions when they mean a for loop. It's not just a "has result vs updates inplace" dichotomy: often it produces some output like a drawing or just a print() call [one of rare things that was better when print was a command is that it was impossible to do this:]. Yes, I know print call is not a method, but e.g. .plot() on DataFrame is. I'd sleep easier if I knew the Programming FAQ didn't encourage this style.

It would be enough to add a sentence of a sort
"If you don't care about the return value of the method, use a for loop.
for obj in mylist: obj.method()
"
msg366979 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-04-22 06:51
This is an antipattern I've seen on multiple occasions in real code. A common offender is:

    [worker.join() for worker in workers]

Similarly, things like:

    [plugin.start() for plugin in plugins]

I do think it would be worth updating the FAQ text.
msg366980 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-04-22 06:54
However, the list comprehension pattern is not as bad as lines like this one [#1]:

    map(lambda plugin: self.start_plugin(plugin), self._plugins)

... which of course stopped working as soon as we transitioned to Python 3. :-(


[#1] https://github.com/enthought/envisage/blob/b7adb8793336dd3859623cb89bcc7bdfefe91b29/enthought/envisage/plugin_manager.py#L105
msg366982 - (view) Author: Vedran Čačić (veky) * Date: 2020-04-22 07:11
Mapping lambdas is always inferior to comprehensions, in fact the main reason comprehensions were introduced was that mapping lambdas was cumbersome. (It didn't work so well for filtering by lambdas, but that's another story.)

As I said, Py3K was beneficial since raw maps weren't eager anymore, but it also gave us a print_function, enabling the new antipattern

    [print(obj) for obj in mylist]

which wasn't possible before.

It's too late for Python, but a lesson for some future programming language: procedures and functions are not the same. Function call is an expression having a value, procedure call is a statement having an effect. Both are useful, but conflating them is not.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84522
2020-04-22 07:11:11vekysetmessages: + msg366982
2020-04-22 06:54:42mark.dickinsonsetmessages: + msg366980
2020-04-22 06:51:40mark.dickinsonsetnosy: + mark.dickinson
messages: + msg366979
2020-04-20 23:18:37vekysetnosy: + veky
messages: + msg366901
2020-04-20 21:17:11rhettingersetnosy: + rhettinger
messages: + msg366885
2020-04-20 21:06:38Dominik V.create