4 ms·
I'm not sure what you mean by the first half of your comment. Both def and lambda create functions, and quoting from the article "Lambda expressions are just a
by detaro 8y ago
I'm not sure what you mean by the first half of your comment. Both def and lambda create functions, and quoting from the article "Lambda expressions are just a special syntax for making functions. They can only have one statement in them and they return the result of that statement automatically.".
def binds it to a name at the same time, which is remembered in the object (which AFAIK isn't really used for anything except maybe debugging tools), lambda returns an anonymous one. The resulting object can be passed around in both cases.
- nikofeyn 8y agoit's because i am not that deeply familiar with python and the article is not clear itself on this. what is he complaining about then if they are identical in what's returned? is it simply because the REPL returns <lambda> instead of the function name? why does the REPL do this? are they different in some subtle way? the point was in something like f#, lambda creates a function which can then be bound to a name if you want. if you do so, this is identical to the "normal" function definition because that is really just syntactic sugar for creating lambda functions and then binding it to a name.
- detaro 8y agoSince the objects are identical type, you could set a doc string and a name on a lambda (they're just properties of the object) and see them during program execution/on the REPL, might make sense for generated stuff like classes from an ORM?
- dragonwriter 8y ago> is it simply because the REPL returns <lambda> instead of the function name? why does the REPL do this? Because lambdas don't have function names.
- nikofeyn 8y agoi know that. in the example, he assigned the lambda to normalize_case.
- detaro 8y agoSure, but an object doesn't know what variable it is assigned to (and which variable name should it pick if there's multiple). the def syntax merely sets the name property on the function object too, since it has a name to set available. As said in other comments, nice for interactive use, irrelevant otherwise. >>> normalize_case = lambda s: s.casefold() >>> normalize_case <function <lambda> at 0x034B04B0> >>> normalize_case.__qualname__ = "normalize_case" >>> normalize_case <function normalize_case at 0x034B04B0> vs >>> def normalize_case(s): return s.casefold() >>> normalize_case <function normalize_case at 0x00594420> >>> normalize_case.__qualname__ 'normalize_case'
- nikofeyn 8y agothank you for clarifying exactly what's going on. other than the __qualname__, in what other ways do the two different bindings differ from each other? however, this still goes in the bucket of why i personally dislike python. this just seems sloppy and overly complicated. for example, i tested this out myself. > test = lambda x : x*x > test => <function <lambda> at 0x7ff6221d76e0> > test.__qualname__ Traceback (most recent call last): File "python", line 1, in <module> AttributeError: 'function' object has no attribute '__qualname__' > test.__qualname__ = "test" > test => <function <lambda> at 0x7ff6221d76e0> so the repl still reports <lambda> instead of test. actually it turns out that even def doesn't seem to set the __qualname__ property, so i don't get the same thing as you (i am using repl.it which apparently uses python 2.7.10). however, using dir(test) showed that there is the func_name property. > def test2(x): > return x*x > test2.__qualname__ Traceback (most recent call last): File "python", line 1, in <module> AttributeError: 'function' object has no attribute '__qualname__' > test2.func_name => 'test2' > test.func_name => '<lambda>' that seems to be the property that the repl reports, as it gets set by the lambda assignment and the def keyword to the function name. (i verified that by altering the func_name property manually.) yet, somehow, people call python "simple".
- firefwing24 8y agoFYI, repl.it has Python3, you just need to select Python3 and not Python. As far as I know, the __qualname__ property was introduced in Python3.3, so obviously you wouldn't see it in any Python 2 version.
- typomatic 8y agoModules and functions in Python have a __name__ attribute. If you've used Python for scripts, you've probably written `if __name__ == "__main__":` at some point, which is the module name for executed scripts, whereas imported modules get their proper name. Similarly, Python basically keeps track of "proper" names for proper functions, but can't/won't do so for lambdas.
- xapata 8y agoJust as in f#, Python's lambdas can be bound to a name. There's no real difference between a def and a lambda. You're complaining about some distinction that doesn't exist. You didn't even try to discover whether it does before complaining about it.