Proposed keyword to transfer control to another function

Ethan Furman ethan at stoneleaf.us
Fri Jul 17 00:44:30 EDT 2015


On 07/16/2015 04:46 PM, Chris Angelico wrote:

> Examples:
>
> # derived from Paul Rubin's example
> def quicksort(array, start, end):
>       midp = partition(array, start, end)
>       if midp <= (start+end)//2:
>          quicksort(array, start, midp)
>          transfer quicksort(array, midp+1, end)
>       else:
>          quicksort(array, midp+1, end)
>          transfer quicksort(array, start, midp)
>
> def count_usage(func):
>      @functools.wraps(func)
>      def wrapper(*args, **kwargs):
>          wrapper.usage += 1
>          transfer func(*args, **kwargs)
>      wrapper.usage = 0
>      return wrapper
>
> Semantics:
> * Evaluate the function and all its arguments, exactly as per a
> current function call.
> * Leaving them on the stack, remove the current call frame and dispose
> of all its objects.
> * Finally, construct a new stack frame for the target function and
> transfer control to it.
>
> In effect, "transfer f()" is equivalent to "return f()", except that
> the current function finishes before the target is entered.

Sounds cool!  Code it up and let us know how it goes.  :)

--
~Ethan~



More information about the Python-list mailing list