-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Programming FAQ about "How do I apply a method to a sequence of objects?" should include the option of an explicit for-loop #84522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Right now the question is simply answered with:
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:
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. |
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. |
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 |
This is an antipattern I've seen on multiple occasions in real code. A common offender is:
Similarly, things like:
I do think it would be worth updating the FAQ text. |
However, the list comprehension pattern is not as bad as lines like this one [#1]:
... 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 |
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
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. |
gh-94660 adds this. |
…H-94660) (cherry picked from commit 97c493d) Co-authored-by: Samuel Sloniker <[email protected]>
(cherry picked from commit 97c493d) Co-authored-by: Samuel Sloniker <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: