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.
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.
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.
Because lambdas don't have function names.
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'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".
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.
Perhaps it would help if you didn't think of "lambda" as defining a lambda function, but something more prosaic like "defexpr", and that Python doesn't have lambda functions at all?
As to your experiments, you have found one of the many differences between Python 2 and Python 3.
After researching F# for about 10 minutes, perhaps you might think of Python 2 as being like OCaml and Python 3 being like F# - they are two instances of the same language family, with a mutually compatible language subset, but many incompatibilities.
python's version bifurcation problem is its problem, not mine. the problem exists.
You've bitten onto a more or less internal detail (I've TA'ed a bunch of undergrad courses in Python, and it's firmly in the land of things students don't know if they don't discover it on their own) and use that and seemingly quite little grasp of how Python works to argue that it isn't simple, while of course languages you are more familiar with are better.
Similarly, Python basically keeps track of "proper" names for proper functions, but can't/won't do so for lambdas.