I have a question about handing over multiple factors to a function (dynamically)

Asked 2 years ago, Updated 2 years ago, 21 views

In C++/C,

fn(a, b)
fn(a, b, c, d, ...)

I handed over several factors by overloading or ... like this, but I wonder how it can be used in Python.

python

2022-09-21 14:19

1 Answers

Use the * operator if you do not accept the keyword factor.

def manyArgs(*arg):
    print ("I was called with", len(arg), "arguments:", arg)


manyArgs(1) #I was called with 1 arguments: (1,)
manyArgs(1, 2,3)#I was called with 3 arguments: (1, 2, 3)

As you can see, all the factors are returned to the tuple type.

For more information, what are the ** and * in the parameters?Please refer to


2022-09-21 14:19

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.