5 ms·
it'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 w
by nikofeyn 8y ago
it'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.
- 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.
- 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.