Originally ~~ or originally ~~ *
and ** Established as
. However, it seems that they are usually written as * args
and ** kwargs
in the neighborhood. The main difference is whether the variable completion method is tuple
or dictionary
.
*args
def foo(*args):
for a in args:
print a
foo(1) #1
foo(1,2,3) #1\n2\n3\n
If you put * args
as a temporary argument, when you say" how many arguments to take yet "during function creation"
ʻArgs` is responsible for taking all the arguments that come in as tuple placeholders.
def test_var_args_call(arg1, arg2, arg3):
print ("arg1:", arg1)
print ("arg2:", arg2)
print ("arg3:", arg3)
args = ("two", 3)
test_var_args_call(1, *args)
#result
arg1: 1
arg2: two
arg3: 3
An image of preparing the ʻargsvariable first and then covering it with
* args. The first
parameter: 1 is received as ʻarg1
and the rest are all placed inside ʻarg2 and ʻarg3
. By the way, if you specify the number of arguments first and then enter it as a function, be aware that you can only receive as many arguments as the function defined first can receive. If it is def function (arg1, * args)
, only the first one goes to ʻarg1 and the rest is taken by
* args`.
def test_var_args(farg, *args):
print "formal arg:", farg
for arg in args:
print "another arg:", arg
test_var_args(1, "two", 3)
#result
formal arg: 1
another arg: two
another arg: 3
If you want to use it like this, you often don't know how many arguments will come, so it's easier to say "process x for the number of arguments that come out" with for loop
. It's a small detail, but if you use return
instead of print
here, only the first one will be displayed. I think the reason is probably that return
has the characteristic that once it is processed, the loop ends. As a countermeasure that comes to mind, save it in a list inside the loop and execute __return statement
outside the __ loop.
def test_var_args(farg, *args):
print("formal arg:", farg)
for arg in args:
print("another arg:", arg)
return arg
#result
formal arg: 1
another arg: two
another arg: 3
3
I tried to force it like this, but I could only get the last three. The reason is that the loop ends with the end of ʻarg` sticking to 3.
The for-loop runs each statement in it for however so many times.. if one of your statements is a return, then the function will return when it hits it. This makes sense in, for example, the following case:
def get_index(needle, haystack):
for x in range(len(haystack)):
if haystack[x] == needle:
return x
Here, the function iterates until it finds where the needle is in the haystack, and then returns that index (there's a builtin function to do this, anyways). If you want the function to run for however many times you tell it to, you have to put the return AFTER the for-loop, not inside it, that way, the function will return after the control gets off the loop
def add(numbers):
ret = 0
for x in numbers:
ret = ret + x
return ret # needs to be outside the loop
**kwargs
In a nutshell, *
manages variables with tuples, while **
saves them as a dictionary. In other words, you can take two arguments, key
and value
.
def bar(**kwargs):
for a in kwargs:
print(a, kwargs[a])
bar(name="your mom", age=12, hotness=1) #hotness 1\nage 12\nname your mom
*l
In addition to * args
and ** kwargs
, there is also an idiom called * l
. The main usage is to open the list received as a variable, extract the contents and save it in tuple
. All you have to do is add a list to the value of the variable l
(or define it and send it later, etc.) and just call it withfunc (* l)
. However, it is necessary to prepare the same number of parameter
s for the function to be called.
def foo(a, b):
print(a, b)
l = [1, 2]
foo(*l) #1, 2
By the way, both * args
and ** kwargs
can only be received as targets that receive functions. What does that mean
def make_averaged(*args):
return *args / len(str(*args))
# causes syntax error
Since it causes syntax error, handle variables only with the name after *
. As a memorandum because I sometimes forget it.