function wrappers

R

Ramon Crehuet

Dear all,
I have a Fortran programming background and I have some difficulties in
understading how function wrappers work. I have delved into the subject
because of the new decorators, but I understand the decorator syntax. My
ignorance is more profound... Here is an example without decorators:

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?
I have looked into some books and google but I cannot find a
satisfactory explanation. Could somebody please give me a hint or point
me to an appropriate source?
Thanks for your time,

Ramon
 
M

Marc 'BlackJack' Rintsch

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
 
P

Paul Hankin

Dear all,
I have a Fortran programming background and I have some difficulties in
understading how function wrappers work. I have delved into the subject
because of the new decorators, but I understand the decorator syntax. My
ignorance is more profound... Here is an example without decorators:

Functions are first-class values: this means they can be passed
around, used in expressions, and returned from other functions in just
the same way that ints, floats and other more obvious types.

All a decorator is a function that takes the function you give it, and
returns a new function. In your 'require_int' example, it returns the
nested function 'wrapper'. The clever bit is that 'wrapper' remembers
the argument to the decorator - func - (with value p1 in your
example), and can call it later.

Think of the 'def' statement as constructing an object which happens
to be a function. It has a dictionary which is used for variable
lookup so that it can remember 'func'. The def statement also binds
this function object to a name: the name of the function. You can see
some of the gory details if you go:

def f():
print 42

dir(f)

.... list of attributes of the function object.
 
S

Scott David Daniels

Ramon said:
def require_int(func):
def wrapper(arg):
assert isinstance(arg, int)
return func(arg)
return wrapper
def 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?
I suspect you don't understand that each time require_int is called
a _new_ function named wrapper is created (and then returned).

-Scott David Daniels
Scott David (e-mail address removed)
 
B

Bruno Desthuilliers

Scott David Daniels a écrit :
I suspect you don't understand that each time require_int is called
a _new_ function named wrapper is created

which remembers the 'environnement' in which it was defined - this is
known as a 'closure'.
(and then returned).

And when this wrapper function is called (instead of the original), it
receives the arg, test it, and if approriate delegate call to the
original (wrapped) function.

Another way (perhaps easier to understand if you don't have any
experience with higher order functions and other functional programming
idioms) to get the same result would be:

class require_int(object):
def __init__(self, func):
self._func = func
def __call__(self, arg):
assert isinstance(arg, int)
return self._func(arg)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top