[英]How Can I Define a Function Whose Parameters Change According to Input File?
I have a CBR function which takes 4 parameters like我有一个 CBR function 它需要 4 个参数,例如
CBR (w1,w2,w3,w4,x_train,y_train) w1 to wn changes according to the input file. CBR (w1,w2,w3,w4,x_train,y_train) w1 到 wn 根据输入文件变化。 w1 to wn are weights of independent parameters. w1 到 wn 是独立参数的权重。 Thus, according to the input file and the size of the input parameters, w1 to wn changes (N size of input parameters).因此,根据输入文件和输入参数的大小,w1 到 wn 会发生变化(输入参数的大小为 N)。 How can I define such a function?如何定义这样的 function? Could you give me an example?你能给我举个例子吗? Thanks谢谢
To pass a variable number of arguments you can use要传递可变数量的 arguments,您可以使用
def CBR(x_train, y_train, *args):
...
The special syntax *args
is used to pass a variable number of arguments to a function.特殊语法*args
用于将可变数量的 arguments 传递给 function。
The syntax is to use the symbol *
to take in a variable number of arguments;语法是使用符号*
来接收可变数量的 arguments; by convention, it is often used with the word args
but you can use w
if you prefer.按照惯例,它通常与args
一词一起使用,但如果您愿意,也可以使用w
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.