473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

function wrappers

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
Oct 10 '07 #1
3 1138
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
Oct 10 '07 #2
On Oct 10, 11:39 am, Ramon Crehuet <rcs...@iiqab.csic.eswrote:
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.

--
Paul Hankin

Oct 10 '07 #3
Ramon Crehuet wrote:
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 Da*****@Acm.Org
Oct 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
3
by: Paul Rudin | last post by:
I'm having a problem with the python wrappers generated from a type library. The symptom is that I get failures to find connections points inside the wrappers when called from DispatchWithEvents...
18
by: Adrian B. | last post by:
Does anyone know of a framework or library that will enable me to use publish/subscribe comms? I want to create a server (using Python) running on a Unix box that will accept client connections...
3
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
51
by: thesushant | last post by:
hi, can anyone of C masters siting out there suggest any 1 example of re-entrnt function just to show what is the significance of that property and how we can exploit it .... sushant
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
1
by: TC | last post by:
Every time I open my project, I get a warning which says "There are updated custom wrappers available for the following referenced components: Office." When I double-click on the warning, I get...
9
by: tai | last post by:
Hi. I'm looking for a way to define a function that's only effective inside specified function. Featurewise, here's what I want to do: bar_plugin_func = function() { ...; setTimeout(...);...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.