3 ms·
Readability and scoping are two big advantages. Anonymous functions are often short and sweet, but also highly specific to a single section of the code. It's no
by phailhaus 3y ago
Readability and scoping are two big advantages. Anonymous functions are often short and sweet, but also highly specific to a single section of the code. It's not meant to be reused. Having to name that one-off use case and hoist it into a function elsewhere makes it harder to maintain and understand.
With proper anonymous functions, you can just read through it in one go.
- PurpleRamen 3y agoBut you don't need to name it big or maintain it somewhere else? Just use a throwaway name and write it where you use it. I mean python even allows overriding functions with the same name in the same scope. You could literally go with using just the same name everywhere.
- biorach 3y agoTrue, but you're missing the point.
- PurpleRamen 3y agoThen what is the point?
- phailhaus 3y agoReadability. If you're using the same name everywhere, then your code is really hard to understand for other devs: val = my_list.reduce(foobar) What is even going on here?
- cmcconomy 3y agoone option is to define your function inline, directly above the line where you would have liked to write the complex anonymous function. I guess if you are worried about namespace pollution you can 'del' the symbol afterward.