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

2020-04-22 Thread Vedran Čačić

Vedran Čačić  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-22 Thread Mark Dickinson


Mark Dickinson  added the comment:

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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-22 Thread Mark Dickinson


Mark Dickinson  added the comment:

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.

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-20 Thread Vedran Čačić

Vedran Čačić  added the comment:

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()
"

--
nosy: +veky

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-20 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

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.

--
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2020-04-20 Thread Dominik V.


New submission from Dominik V. :

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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com