function wrappers

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Oct 10 07:42:15 EDT 2007


On Wed, 10 Oct 2007 12:39:24 +0200, Ramon Crehuet wrote:

> def require_int(func):
>        def wrapper(arg):
>            assert isinstance(arg, int)
>            return func(arg)
>        return wrapper
> p1(a):
>    print a
> 
> p2=require_int(p1)
> 
> My question is: why do p2 arguments become wrapper arguments? What is 
> the flux of the arguments in the program when you pass functions as 
> arguments?

The function `p1` is passed into `require_int`.  It is bound to the local
name `func` in `require_int`.  Everytime `require_int` is called a new
function object is created and bound to the local name `wrapper`.  The
name `func` in that new function object refers to the object bound to
`func` in the `require_int` namespace.  Then the new function is returned
still carrying a reference to the `func` object that was passed into
`require_int`.

Ciao,
	Marc 'Blackjack' Rintsch



More information about the Python-list mailing list