Posts: 3
Threads: 1
Joined: Sep 2018
It would be nice if python supported this kind of "expression comprehension", or do it?
I frecuently encounter situations in which I need to execute some function for a list of things, or a range of whatever, and wonder if it could be done in a one-liner like the message title. I know we have map's, and for's but I think this feels more natural and "pythonic"
Is there a syntax I'm not aware of? Or maybe someone proposed this in the past but coulnd't be done for any unwanted effects?
Posts: 2,953
Threads: 48
Joined: Sep 2016
_ = [do_something(obj) for obj in iterable if is_valid(obj)] This is the same as it's in your topic.
Posts: 591
Threads: 26
Joined: Sep 2016
Sep-23-2018, 11:35 AM
(This post was last modified: Sep-23-2018, 11:35 AM by Mekire.)
If you aren't trying to generate a list (or whatever sequence) then it is absolutely NOT more pythonic to use a comprehension. Using a comprehension for a side effect is an anti-pattern.
For example, the following is an example of what NOT to do:
[print(x) for x in range(10)] You aren't actually interested in the list you create, and are abusing comprehension syntax (which is completely unnecessary).
So, do you actually want the list or do you just want a function to run several times with different inputs?
Posts: 3
Threads: 1
Joined: Sep 2018
Obviously, the point here isn't to make a list (which is trivial), but to be able to execute commands in a "comprehension fashion".
See the title.
Thanks
Posts: 3,458
Threads: 101
Joined: Sep 2016
But that is what's happening. It's exactly the same as map/filter, if you consume all of the input at once: >>> def do_something_with(x):
... print(x**2)
...
>>> def is_valid(x):
... return x % 2 == 0
...
>>> things = range(10)
>>> [do_something_with(x) for x in things if is_valid(x)]
0
4
16
36
64
[None, None, None, None, None]
>>> list(map(do_something_with, filter(is_valid, things)))
0
4
16
36
64
[None, None, None, None, None]
Posts: 591
Threads: 26
Joined: Sep 2016
(Sep-24-2018, 07:05 AM)pystag Wrote: Obviously, the point here isn't to make a list (which is trivial), but to be able to execute commands in a "comprehension fashion". And that is exactly my point. You shouldn't be using a comprehension. Yes, you can write a comprehension that does exactly what you want, but it isn't encouraged. If you care about being "pythonic" write a standard for loop and use multiple lines. If you don't care, knock-yourself out and shove it all into a one line list comp.
Posts: 2,953
Threads: 48
Joined: Sep 2016
(Sep-24-2018, 07:12 AM)nilamo Wrote: But that is what's happening. He can do it in the way as my prev. post but I don't see any benefits. Readability of the code means something.
Posts: 3
Threads: 1
Joined: Sep 2018
(Sep-24-2018, 07:33 AM)Mekire Wrote: You shouldn't be using a comprehension. Yes, you can write a comprehension that does exactly what you want, but it isn't encouraged. If you care about being "pythonic" write a standard for loop and use multiple lines. If you don't care, knock-yourself out and shove it all into a one line list comp.
I agree I shouldn't be using a comprehension with the current python syntax. But wouldn't be nice if python itself implemented it? That's exactly the point of my post  Did someone ever make this suggestion to dev team?
inc(x) for x in range(5)
process(apply) for apply in my_folder
fix_hunger(country) for country in world if country is not in fed_countries
...etc...
Posts: 3,458
Threads: 101
Joined: Sep 2016
I really don't understand. You're saying you don't want a list comprehension, but what you're describing is a list comprehension, and all of your examples are legal syntax for list comprehensions or generator expressions. In what way is your suggestion different from what we already have?
|