|
Why 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. |