Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to force a lambda function to return an object rather than the None value of the last method on the object?

Sometimes I cannot use a lambda function on an object because the last method applied to the object returns None. What is the trick to be able to return the object itself instead of the method returned value?

Example, I want to get the updated copy of the dictionary instead of None:

dict_update = lambda d, upd: d.copy().update(upd)

d = dict(lw=3, ls='-')
upd = dict(c='C0', lw=4)
d1 = dict_update(d, upd)
for v in [d, upd, d1]: print(v)

{'lw': 3, 'ls': '-'}
{'c': 'C0', 'lw': 4}
None

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use dict unpacking and packing:

dict_update = lambda d, upd: {**d, **upd}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading