Re: Tuple unpacking inside lambda expressions
On 20/04/2022 13:01, Sam Ezeh wrote: I went back to the code recently and I remembered what the problem was. I was using multiprocessing.Pool.pmap which takes a callable (the lambda here) so I wasn't able to use comprehensions or starmap Is there anything for situations like these? Hm, I don't see pmap, but there is a starmap(): https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.starmap -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
On Apr 20, 2022 13:01, Sam Ezeh wrote: I went back to the code recently and I remembered what the problem was. I was using multiprocessing.Pool.pmap which takes a callable (the lambda here) so I wasn't able to use comprehensions or starmap Is there anything for situations like these? = Could it simply be: multiprocessing.Pool.pmap(lambda job: result.process(*job), jobs) -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
I went back to the code recently and I remembered what the problem was. I was using multiprocessing.Pool.pmap which takes a callable (the lambda here) so I wasn't able to use comprehensions or starmap Is there anything for situations like these? Kind Regards, Sam Ezeh On Sat, 16 Apr 2022 at 22:36, Sam Ezeh wrote: > > Two questions here. > > Firstly, does anybody know of existing discussions (e.g. on here or on > python-ideas) relating to unpacking inside lambda expressions? > > I found myself wanting to write the following. > > ``` > map( > lambda (module, data): result.process(module, data), > jobs > ) > ``` > However, it's of course not legal Python syntax. > > The following were potential options but I felt they removed some of > the meaning from the code, making it less understandable for other > people. > > ``` > map( > lambda job: result.process(job[0], job[1]), > jobs > ) > ``` > > ``` > map( > lambda job: result.process(*job), > jobs > ) > ``` > > Secondly, for situations like these, do you have any go-to methods of > rewriting these while maintaining clarity? > > Kind Regards, > Sam Ezeh -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
This also works great! Kind Regards, Sam Ezeh On Tue, 19 Apr 2022 at 12:03, Antoon Pardon wrote: > > Op 16/04/2022 om 23:36 schreef Sam Ezeh: > > Two questions here. > > > > Firstly, does anybody know of existing discussions (e.g. on here or on > > python-ideas) relating to unpacking inside lambda expressions? > > > > I found myself wanting to write the following. > > > > ``` > > map( > > lambda (module, data): result.process(module, data), > > jobs > > ) > > ``` > > However, it's of course not legal Python syntax. > > Why not write: > > itertools.starmap(result.process, jobs) > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
Op 16/04/2022 om 23:36 schreef Sam Ezeh: Two questions here. Firstly, does anybody know of existing discussions (e.g. on here or on python-ideas) relating to unpacking inside lambda expressions? I found myself wanting to write the following. ``` map( lambda (module, data): result.process(module, data), jobs ) ``` However, it's of course not legal Python syntax. Why not write: itertools.starmap(result.process, jobs) -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
> In general, if you're using map() with a lambda function, it's often simpler to switch to a comprehension. Oh, of course, completely went past my head. > [result.process(module, data) for module, data in jobs] And this works great, thanks! On Sat, 16 Apr 2022 at 22:42, Chris Angelico wrote: > > On Sun, 17 Apr 2022 at 07:37, Sam Ezeh wrote: > > > > Two questions here. > > > > Firstly, does anybody know of existing discussions (e.g. on here or on > > python-ideas) relating to unpacking inside lambda expressions? > > > > I found myself wanting to write the following. > > > > ``` > > map( > > lambda (module, data): result.process(module, data), > > jobs > > ) > > ``` > > However, it's of course not legal Python syntax. > > What about: > > [result.process(module, data) for module, data in jobs] > > (or with () instead of [] around the outside if you want a generator)? > > In general, if you're using map() with a lambda function, it's often > simpler to switch to a comprehension. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Tuple unpacking inside lambda expressions
On Sun, 17 Apr 2022 at 07:37, Sam Ezeh wrote: > > Two questions here. > > Firstly, does anybody know of existing discussions (e.g. on here or on > python-ideas) relating to unpacking inside lambda expressions? > > I found myself wanting to write the following. > > ``` > map( > lambda (module, data): result.process(module, data), > jobs > ) > ``` > However, it's of course not legal Python syntax. What about: [result.process(module, data) for module, data in jobs] (or with () instead of [] around the outside if you want a generator)? In general, if you're using map() with a lambda function, it's often simpler to switch to a comprehension. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Tuple unpacking inside lambda expressions
Two questions here. Firstly, does anybody know of existing discussions (e.g. on here or on python-ideas) relating to unpacking inside lambda expressions? I found myself wanting to write the following. ``` map( lambda (module, data): result.process(module, data), jobs ) ``` However, it's of course not legal Python syntax. The following were potential options but I felt they removed some of the meaning from the code, making it less understandable for other people. ``` map( lambda job: result.process(job[0], job[1]), jobs ) ``` ``` map( lambda job: result.process(*job), jobs ) ``` Secondly, for situations like these, do you have any go-to methods of rewriting these while maintaining clarity? Kind Regards, Sam Ezeh -- https://mail.python.org/mailman/listinfo/python-list