4 ms·
> This is maybe a bit far from the topic, but I would like to know why there is such a need for anonymous functions in Python. Building new control structures
by masklinn 15y ago
> This is maybe a bit far from the topic, but I would like to know why there is such a need for anonymous functions in Python.
Building new control structures in-language, without having to hack the interpreter, and without the result looking like somebody has puked all over your screen. First-class functions are under-used in Python because they're syntactically and semantically heavy. With full anonymous functions, the `with` statement would not have been needed, it could have been handled through a higher-order function, akin to Haskell's `bracket`[0]
> In Javascript, with silent overwriting of functions and without modules, it is obviously very bad to pollute the only namespace with function names that can clash with each other
Namespace pollution is completely irrelevant (it's very easy to handle it in Javascript via the namespace pattern).
> Moreover, as it is lame to use _temp as function name, it forces you to formulate a meaningful function name, which is good pratice, especially for your lectorate, right?
If it is, why can I put name-less blocks of code in `for`, `if`, `while` and `with` bodies?
Not all code blocks need a name. Those that do will be given one.
[0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exception-Base.html#v:bracket http://hackage.haskell.org/packages/archive/base/latest/doc/...
- thurn 15y agoIn fact, Haskell style frowns on using too many lambdas. It's generally felt that adding named functions is better for debugging and readability, the same as in python. So with the bracket function, for example, it's still considered better practice to pass named functions to it.
- masklinn 15y ago> In fact, Haskell style frowns on using too many lambdas. It's generally felt that adding named functions is better for debugging and readability, the same as in python That's really besides my point. And last time I checked, Haskell's `if` and `case` expressions did not take functions, they took anonymous expressions. > So with the bracket function, for example, it's still considered better practice to pass named functions to it. To pass a named function as third argument to bracket?
- deleted 15y ago[deleted]
- MostAwesomeDude 15y ago"First-class functions are under-used in Python" Every function is first-class in Python. Every method may be treated as a generic callable. In fact, you can create first-class generic callables with the __call__ slot of the new-style object data model: class A(object): def __call__(self): print "Hi!" A()() Lambdas in Python are syntactic sugar; the following two statements are equivalent, save for the internal name given to the function (lambdas are given the name "<lambda>" for tracebacks): f = lambda x, y=None, *args, **kwargs: None def f(x, y=None, *args, **kwargs): pass