Yes. You can use *args as a non-keyword argument. You will then be able to pass any number of arguments.
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, Python will unpack the arguments as a single tuple with all the arguments.
For keyword arguments you need to accept those as a separate actual argument, as shown in Skurmedel's answer.
Answer from unwind on Stack OverflowYes. You can use *args as a non-keyword argument. You will then be able to pass any number of arguments.
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, Python will unpack the arguments as a single tuple with all the arguments.
For keyword arguments you need to accept those as a separate actual argument, as shown in Skurmedel's answer.
Adding to unwinds post:
You can send multiple key-value args too.
def myfunc(**kwargs):
# kwargs is a dictionary.
for k,v in kwargs.iteritems():
print "%s = %s" % (k, v)
myfunc(abc=123, efh=456)
# abc = 123
# efh = 456
And you can mix the two:
def myfunc2(*args, **kwargs):
for a in args:
print a
for k,v in kwargs.iteritems():
print "%s = %s" % (k, v)
myfunc2(1, 2, 3, banan=123)
# 1
# 2
# 3
# banan = 123
They must be both declared and called in that order, that is the function signature needs to be *args, **kwargs, and called in that order.
How to pass a variable number of arguments to a function/method inside a method when calling a method, without having to define every single possible argument to be used?
Best way to make a function that requires many arguments
python - Specifying an unlimited number of arguments to a commanline option - Stack Overflow
python - How do I take unlimited sys.argv[] arguments? - Stack Overflow
Videos
I have to create a function that can take any number of arguments. I’m not too sure how to do this, would it just be a function with a variable parameter that can be anything?
Howdy!
Say I have the following class scheme and example code. I can call a.Run_Model() and pass arguments to it, but I want some of those arguments to be passed to GridSearchCV inside a.Run_Model()
Class Test:
def __init__(self):
self.model = LinearRegression()
def Run_Model(self, opt=False):
if opt:
self.model = GridSearchCV(self.model)
a = Test()
# I may want to change the model
a.model = GradientBoostingRegressor(max_depth=4)
a.Run_Model()# how can I run this without having
# to declare the cv variable on GridSearchCV inside Run_Method()?
a.Run_Model(opt=True, GSV_args={'cv':5})I know I can do it by declaring the arguments as def Run_Model(self, cv=5) and subsequently pass them as GridSearchCV(self.model,cv=cv).
But this does not sound too flexible. I'd have to declare ALL GSC variables on two different spots just to pass some variables to the function inside the class. And I'd have to set all of them to some default value just so that nothing breaks if I decide to omit something at any time, or if I end up not wanting to use that particular part of the code.
Is there a way to pass a **kwargs to Run_Model() and have those same **kwargs be applied to GSC inside it without having to define every single possible GSC input?
This is an example as I could declare a.model = GSV outside the class and code to make it work, but I'm just looking for a better and more flexible way to pass arguments around to functions inside methods when calling methods.
I imagine I should pass around the bare minimum of variables this way, but there must be a way to do it that I'm not familiar with. So what can I do? Is it possible to do what I want to do?
Cheers!
I have a function that requires many (~20) arguments, and I'm wondering if there's a better way... For context, I am using gdsfactory to make mask layouts for fabricating semiconductor devices. My function creates the device and returns it as a gdsfactory component object which can be written to a .gds CAD file. The arguments specify all the possible device parameters for fabrication (dimensions, spacing between metal connections, etc. etc.), and the function looks something like this:
def my_device(arg1=default_val1, arg2=default_val2, arg3=default_val3,... argN=default_valN):# code to create the device layout using all the argumentsreturn component
In most use cases the default values are fine so I can call the function without specifying arguments, but I still need to have the option to specify any of the device parameters if I want to. I know it's generally considered bad practice to have so many arguments, so I'm curious if there's a better way to accomplish this while still being able to create my component with one function call?