1

I have a function which receives one function argument and that function argument also receives one argument but I don't know how to pass this argument to argument function in python. Here is the sample code:

def pr(text):
 print(text)
def frun(func):
 func()
frun(pr)

The question is how can I pass text argument in frun(pr)? And please consider about using keyword arguments too.

asked Jan 19, 2014 at 15:53

3 Answers 3

2

If you only have one argument, you can do:

def pr(text):
 print(text)
def frun(func, text):
 func(text)
frun(pr, "blah")

Otherwise, you can use variable arguments "magic" like:

def pr(text):
 print(text)
def frun(func, *args):
 func(*args)
frun(pr, "blah")

The latter is far less readable and could lead to weird errors (in this case, it won't even work with more than one parameter passed, because pr expects only one positional argument), still is sometimes necessary. For instance, when you don't know a priori how many parameters your function will need:

def pr1(text):
 print(text)
def pr2(text, note):
 print(text, note)
def frun(func, *args):
 func(*args)
frun(pr1, "foobarbaz")
frun(pr2, "blah", "IT WORKS!")
answered Jan 19, 2014 at 15:55
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but your answer isn't good on key argument and some how doesn't work.
1

TL;DR: Partial Application

The problem boils down to passing func's argument to the scope you want to do the evaluation in. There are a few ways of doing this.

Explicit Parameterization

This get's unwieldy if you have numerous functions, or deep function call trees.

def pr(text):
 print(text)
def frun(func, *func_args):
 func(*func_args)
frun(pr, *pr_args)

Deferred Execution with Lambda

Limitation: You need all the arguments for func available at the same time, when you create the lambda.

def pr(text):
 print(text)
def frun(func):
 func()
frun(lambda: pr(*pr_args))

Partial Application

Here the nice thing is you can do partial application as the object get's passed along. If you partial using named variables then you eliminate the ordering issue of the arguments.

from functools import partial
def pr(text):
 print(text)
def frun(func):
 func()
frun(partial(pr, *pr_args))
answered Jan 19, 2014 at 17:22

Comments

0

You can using lambda function to pass pr function along with parameters to frun function, without changing your code.

def pr(text):
 print(text)
# just another example to show how to pass more than one argument.
def pr2(text, note):
 print(text, note)
def frun(func):
 func()
frun(lambda: pr('Hello'))
frun(lambda: pr2('Hello', 'World'))
Hello
Hello World

Another way that may be useful for you is to return func in frun as callback function, and then call it with parameters:

def pr(text):
 print(text)
# just another example to show how to pass more than one argument.
def pr2(text, note):
 print(text, note)
def frun(func):
 # do some stuff
 return func
frun(pr)('Hello')
frun(pr2)('Hello', 'World')
Hello
Hello World
answered Jan 19, 2014 at 16:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.