3 ms·
Functions are first-class objects. If you want a multi-line lambda thats just a function.
by VectorLock 4y ago
Functions are first-class objects. If you want a multi-line lambda thats just a function.
- code_runner 4y agoIt’s gross to have a function floating off somewhere that has no true reuse value at all and doesn’t live close to where it is used. I like Python but this is probably my biggest gripe.
- bee_rider 4y agoI mean, it is Python, if you are using it you aren’t aiming to be super-lean, right? Apparently it is possible to delete a function. https://stackoverflow.com/questions/10367414/delete-function https://stackoverflow.com/questions/10367414/delete-function
- saila 4y agoYou can delete references to any object in Python, including functions: >>> def f(): None ... >>> del f but it's pointless to try to manually manage memory in Python in most cases. If you're processing huge amounts of data, there are probably better approaches than using `del` and/or `gc.collect()` to keep memory usage down (like batching and/or using subprocesses). My other comment in this thread is relevant too: https://news.ycombinator.com/item?id=34197904 https://news.ycombinator.com/item?id=34197904
- maxbond 4y agoYou can define a function anywhere, including in the scope of another function. So it doesn't need to be floating somewhere far away from where it's used. (This is the idiomatic way to write decorators, for example.)
- saila 4y agoWhy do your anonymous functions have to live far from where they're used? What's the non-aesthetic difference between this: def func(g_func, a, b): g_func(a, b) def scope(): c = "c" def anon(a, b): print(a, b, c) print(id(anon)) return func(anon, "a", "b") and this: def func(g_func, a, b): g_func(a, b) def scope(): # NOTE: The `|a, b|: (...)` syntax is made up c = "c" return func( |a, b|: ( print(a, b, c) ), "a", "b" ) ? Note that the `anon` function in `scope` is only compiled once. You can verify this by copying the first version to a file named `temp.py` and then running `python -m dis temp.py`. You can also run `scope()` twice in a row and see that the same object ID is printed both times. That said, for purely aesthetic reasons, I would like it if Python had some kind of syntax like version 2, but that's tricky with significant whitespace, and I've never found the lack of such syntax to be particularly limiting in Python.
- VectorLock 4y agoI think its gross to try to inline a multiline anonymous function usually inside of what should just be a function call.
- dragonwriter 4y ago> Functions are first-class objects. If you want a multi-line lambda thats just a function. Yes, lambdas are just anonymous functions that are restricted to a single expression, no statements. This is a problem for programming in functional style, because python idiomatically uses statements a lot, and doing a named definition for a single use function is more verbose and less readable in mant cases.
- maxbond 4y agoMy stylistic advice in that case would be to name it _lambda() (note the underscore that solves keyword issues). Though I don't disagree.