[Python-ideas] Access to function objects
tjreedy at udel.edu
Mon Aug 8 00:01:23 CEST 2011
On 8/7/2011 8:10 AM, Guido van Rossum wrote:
>>> For most uses, standard recursion via the name is good enough, it's only a
>>> few corner cases where self-reflection (as I call it) is needed.
> Right. If it were expected that people would start writing recursive
> calls using __function__ routinely, in situations where a name
> reference works, I'd be very unhappy with the new feature.
I am willing to separate the recursion and attribute access use cases
and not advocate that. I agree that
def fact(n): # requires int n >= 0
return n*__function__(n-1) if n else 1
is less readable than fact(n-1)
> (And if
> someone wants to make the argument that recursive calls using
> __function__ are actually better in some way I am willing to
Such calls would be slightly faster by avoiding a name lookup, but
avoiding *any* function call by using iteration should be faster yet.
For linear recursion (at most one call per call), this is usually trivial.
In my book, I am stipulating that namespace manipulations that change
the recursiveness of a function as written are 'forbidden' for the
purpose of interpreting the code presented. This should be assumed or
stated in other similar contexts.
This restricts the recursion use case to multiple recursion (possibly
multiple calls per call) in production library code that is not easily
converted to iteration, is not intended to be read by anyone other than
maintainers, and that might or even is expected to be used in an adverse
Terry Jan Reedy
More information about the Python-ideas