Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在给定函数中使用参数v_Python_Function - Fatal编程技术网

Python 如何在给定函数中使用参数v

Python 如何在给定函数中使用参数v,python,function,Python,Function,因此,我能够编写上述代码 现在,我被要求修改do_Twow函数,使其接受两个参数——一个函数对象和一个值,并调用函数两次,以如下格式将值作为参数传递: def do_twice(f): """ This function calls any function object that is passed to it twice """ f() f() def print_spam(): print('spam') do_twice(print_spam) 我无法理解如

因此,我能够编写上述代码

现在,我被要求修改do_Twow函数,使其接受两个参数——一个函数对象和一个值,并调用函数两次,以如下格式将值作为参数传递:

def do_twice(f):
  """
  This function calls any function object that is passed to it twice
  """
  f()
  f()

def print_spam():
  print('spam')

do_twice(print_spam)

我无法理解如何在函数中使用参数v

您需要使函数包含两个参数:

def do_twice(f, v):
"""
This function calls any function object that is passed to it twice passing v as the argument

Input:
  f: function object
  v: value
"""
### Write your code here
然后可以使用函数和值调用它:

def do_twice(f, v):
例如:

do_twice(some_func, "a string")

您需要使函数接受两个参数:

def do_twice(f, v):
"""
This function calls any function object that is passed to it twice passing v as the argument

Input:
  f: function object
  v: value
"""
### Write your code here
然后可以使用函数和值调用它:

def do_twice(f, v):
例如:

do_twice(some_func, "a string")

您已经知道如何使用传入参数
f
(直接作为
f
),如何调用传入函数(即
f()
),以及调用函数时如何传递参数(
dou两次(print\u spam)
)。使用传入参数
f
v
时,为了在调用
f
时传递
v
,您有什么具体问题?您已经知道如何使用传入参数
f
(直接作为
f
),如何调用传入函数(即
f()
)以及调用函数时如何传递参数(
do\u两次(print\u spam)
)。使用传入的参数
f
v
,在调用
f
时,为了传递
v
,您有什么具体问题?