New submission from Dominik V. <[email protected]>:
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@python
components: Documentation
messages: 366880
nosy: Dominik V., docs@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 <[email protected]>
<https://bugs.python.org/issue40342>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com