4 ms·
i know that. in the example, he assigned the lambda to normalize_case.
by nikofeyn 8y ago
i 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.
- eesmith 8y agoVan Rossum in https://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html https://python-history.blogspot.com/2009/04/origins-of-pytho... comments that "lambda" was likely the wrong choice for a name, saying "the choice of terminology had many unintended consequences. For instance, users familiar with functional languages expected the semantics of lambda to match that of other languages. As a result, they found Python’s implementation to be sorely lacking in advanced features." 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.
- xapata 8y agoDon't complain about design warts if you're using an old version. Python 2 is just a couple years from end-of-life, and that's after the support guarantee was extended.
- nikofeyn 8y agoi didn't choose python 2.7. it is what i have installed on my work computer because the three projects i have worked on at work that used python were all using python 2.7, and the projects were led by "python people". in a couple cases, it was me who got them to upgrade from 2.6 to at least 2.7, and i have tried communicating to people to use python3 if they insist on python. i wouldn't choose python for a new project unless i had to again help someone else's project. python's version bifurcation problem is its problem, not mine. the problem exists.