On Sat, 22 Sep 2007 14:09:13 -0700, Paul Rubin wrote: >> Nevertheless, def is never a real anonymous function constructor. > > Well, def constructs a function with a name, but the function can stay > around after the name goes away, after which I'd say the function is > nameless. One could otherwise say that (lambda x: x+x) is not anonymous > either, since id(lambda ...) is a unique label stuck to it like a > __name__.
If you want to be tediously pedantic, lambda doesn't construct anonymous functions either: you can bind them to a name, and whether you do or not, they have a name. >>> f = lambda: 3 >>> f.__name__ '<lambda>' It's just that all functions created by lambda share the same name. If you really want an anonymous function... >>> def foo(): ... return 3 ... >>> foo.__name__ = '' >>> foo <function at 0x97a26bc> I know, I know, I'm being tediously pedantic... and that's not what the anonymity of lambda refers to. What it actually means is that the lambda doesn't create a name in a namespace. In that regard, calling a factory function is also anonymous, because the function isn't added to the calling code's namespace. Or, to put it another way... def factory(): def foo(): return 3 return foo Within factory(), foo() is not an anonymous function, because 'foo' is in the local namespace. But the result of factory() is anonymous in the same sense that lambda is: although the function object has a attribute __name__ set to 'foo', calling factory() doesn't modify the caller's namespace (unless you assign the result to a name). -- Steven. -- http://mail.python.org/mailman/listinfo/python-list