Would Anonymous Functions Help in Learning Programming/Python?
Paul Rubin
http
Sat Sep 22 17:09:13 EDT 2007
Bryan Olson <fakeaddress at nowhere.org> writes:
> How anonymous is that function when we can see that its name is 'h'?
h is out of scope after compose returns, so the function is anonymous
in the sense that there is no symbol bound to the function, through
which you can refer to it.
> import math
>
> f = compose(math.sin, math.cos)
> print f.__name__
I prefer to think of __name__ as just some debugging info stuck inside
the closure, though actually Python is introspective enough to be able
to let you locate and call a function whose __name__ is "h". Of
course there might be more than one:
f = compose(math.sin, math.cos)
g = compose(math.sqrt, math.tan)
print f.__name__, g.__name__
> 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 our concern is Python's suitability for studying principles of
> programming, I think I'm on stronger ground. Python is great for
> getting things done. It is easy to learn in the sense of learning
> to to use if for practical tasks. Python's actual semantics are not
> as neat and clean as some other languages.
Fair enough.
More information about the Python-list
mailing list