简体   繁体   中英

send arguments for function arguments in python

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.

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!")

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))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM