+ 3

Which is the best version of this code?

i write two codes.Both do se work but approach is different.i want to know which is the best approach. generally this is switch case in python. 1st approach. def h(string,x,y): return { 'add':x+y, 'mul':x*y, 'div':x/y, }.get(string,None) 2nd approach def h(string,x,y): return { 'add':lambda :x+y, 'mul':lambda :x*y, 'div':lambda :x/y, }.get(string, lambda :None)() which is the best version please tell me.

21st Oct 2018, 2:23 AM
Maninder $ingh
Maninder $ingh - avatar
4 Antworten
+ 1
I'd use the first version as the second one is unnecessarily complex. Theoretically, return lambda : None does the same as return None, but there really isn't any advantage to defining a temporary function that doesn't do anything but return None 🤔
21st Oct 2018, 5:17 AM
Anna
Anna - avatar
+ 3
Anna you are absolutely right. i also choose this.
22nd Oct 2018, 10:19 AM
Maninder $ingh
Maninder $ingh - avatar
0
first
21st Oct 2018, 7:03 AM
Bhavya Champaneri
Bhavya Champaneri - avatar
- 1
1st
1st Nov 2018, 3:21 AM
Yash Vaghasia
Yash Vaghasia - avatar