Python 动态函数参数
Python dynamic function parameters
调用下面的函数时,我可以提供将在函数中代替默认参数使用的值(见下文)。
cerebro.addstrategy(GoldenCross, fast=10, slow=25)
这对于少量已知参数非常有效,但我正在转向更复杂的系统。本质上,我需要传递 fast_1、fast_2、fast_3 等...。这些参数的总量会发生变化(始终在 100 左右,但可以变化)。是否有我可以编写的语句,可以动态地将 X 数量的参数添加到我的函数调用中?
我尝试在函数调用中使用 for 语句,但收到语法错误。
使用 *
怎么样?
def addstrategy(GoldenCross, *fast, slow = 25):
可以举个例子。
>>> def foo(a, *b, c = 36):
print(a, b, c)
>>> foo(1, 2, 3, 4, 5)
1 (2, 3, 4, 5) 36
然而,在这种情况下,您需要初始化 fast
。
两种方式:在参数上使用 *
来使用可变数量的参数,或者将参数视为可迭代的。
def fun1(positional, optional="value", *args):
print(args) # args here is a tuple, since by default variable number of args using * will make that parameter a tuple.
def fun2(positional, args, optional="value"):
print(args) # args here will be dependant on the argument you passed.
fun1("some_value", "value", 1, 2, 3, 4, 5) # args = (1, 2, 3, 4, 5)
fun2("some_value", [1, 2, 3, 4, 5]) # args = [1, 2, 3, 4, 5]
我从两个方面理解了你的问题:
- 你想调用你的函数传递不同的参数(可选),你可以这样完成:
def add(first, second=0, third=3):
return (first+second+third)
number_list = list(range(1, 200)) # Generates a list of numbers
result = [] # Here will be stored the results
for number in number_list:
# For every number inside number_list the function add will
# be called, sending the corresponding number from the list.
returned_result = add(1,second=number)
result.insert(int(len(result)), returned_result)
print(result) # Can check the result printing it
- 您希望您的函数处理任意数量的可选参数,因为您不知道有多少方法可以确定它们的数量,您可以发送一个列表或参数,如下所示:
def add(first,*argv):
for number in argv:
first += number
return first
number_list = (list(range(1, 200))) # Generates a list of numbers
result = add(1,*number_list) # Store the result
print(result) # Can check the result printing it
Here 您可以找到有关 *args
的更多信息
调用下面的函数时,我可以提供将在函数中代替默认参数使用的值(见下文)。
cerebro.addstrategy(GoldenCross, fast=10, slow=25)
这对于少量已知参数非常有效,但我正在转向更复杂的系统。本质上,我需要传递 fast_1、fast_2、fast_3 等...。这些参数的总量会发生变化(始终在 100 左右,但可以变化)。是否有我可以编写的语句,可以动态地将 X 数量的参数添加到我的函数调用中?
我尝试在函数调用中使用 for 语句,但收到语法错误。
使用 *
怎么样?
def addstrategy(GoldenCross, *fast, slow = 25):
可以举个例子。
>>> def foo(a, *b, c = 36):
print(a, b, c)
>>> foo(1, 2, 3, 4, 5)
1 (2, 3, 4, 5) 36
然而,在这种情况下,您需要初始化 fast
。
两种方式:在参数上使用 *
来使用可变数量的参数,或者将参数视为可迭代的。
def fun1(positional, optional="value", *args):
print(args) # args here is a tuple, since by default variable number of args using * will make that parameter a tuple.
def fun2(positional, args, optional="value"):
print(args) # args here will be dependant on the argument you passed.
fun1("some_value", "value", 1, 2, 3, 4, 5) # args = (1, 2, 3, 4, 5)
fun2("some_value", [1, 2, 3, 4, 5]) # args = [1, 2, 3, 4, 5]
我从两个方面理解了你的问题:
- 你想调用你的函数传递不同的参数(可选),你可以这样完成:
def add(first, second=0, third=3):
return (first+second+third)
number_list = list(range(1, 200)) # Generates a list of numbers
result = [] # Here will be stored the results
for number in number_list:
# For every number inside number_list the function add will
# be called, sending the corresponding number from the list.
returned_result = add(1,second=number)
result.insert(int(len(result)), returned_result)
print(result) # Can check the result printing it
- 您希望您的函数处理任意数量的可选参数,因为您不知道有多少方法可以确定它们的数量,您可以发送一个列表或参数,如下所示:
def add(first,*argv):
for number in argv:
first += number
return first
number_list = (list(range(1, 200))) # Generates a list of numbers
result = add(1,*number_list) # Store the result
print(result) # Can check the result printing it
Here 您可以找到有关 *args
的更多信息