[New-bugs-announce] [issue40342] Programming FAQ about "How do I apply a method to a sequence of objects?" should include the option of an explicit for-loop

Dominik V. report at bugs.python.org
Mon Apr 20 17:06:38 EDT 2020


New submission from Dominik V. <dominik.vilsmeier1123 at gmail.com>:

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.

----------
assignee: docs at python
components: Documentation
messages: 366880
nosy: Dominik V., docs at python
priority: normal
severity: normal
status: open
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
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40342>
_______________________________________


More information about the New-bugs-announce mailing list