This article is half an article I created for my notes. However, in order to make a memo that is easy to understand when I look back I tried to make the article easy for anyone to see. (It seems that he will be pointed out when he makes a mistake ... (real intention))
Arguments are roughly classified into two types: formal arguments and actual arguments. Formal argument: Used in function ** definition **. A formal argument because it gives a formal name to the actual object. (argument) Actual argument: Used in function ** call **. Since it is an actual object, it is an actual argument. (parameter)
def foo(a): #Formal argument
a+=1
return a
print(foo(1)) #Actual argument
>>>2
An important property of formal parameters is that they are ** set for each function call **. Let's look at a concrete example.
def remove_first(lst):
lst = lst[1:]
print(lst)
lst = [1, 2, 3, 4]
print(lst)
remove_first(lst)
>>> [2, 3, 4]
remove_first(lst)
>>> [2, 3, 4]
What I want to say here is that the results of the first and second remove_first (lst) are the same. In other words, "the formal argument when called for the first time" and "the formal argument when called for the second time" are It means that even the same variable is treated as a different variable.
There are five patterns for the formal parameters mentioned above.
--Position or keyword: so-called ordinary function definition --Position only: See below --Keywords only: See below --Variable length position: See below --Variable length keywords: See below
For the time being, here are the usual positions or keywords.
def foo2(a, b=3): #a is a positional argument and b is a keyword argument
return a + b
def foo3(a=1, b): #Error (keyword argument cannot be set before positional argument)
return a + b
print(foo2(1)) #Positional argument (The default value at the time of function definition is applied to the value of b)
>>>4
print(foo2(1,5)) #Positional argument (a=1,b=5)
>>>6
print(foo2(1,b=5)) #Positional and keyword arguments
>>>6
print(foo2(a=1,b=5)) #Keyword arguments
>>>6
print(foo2(b=2)) #error
>>>TypeError
print(foo2(b=3,a=1)) #Change the order of keyword arguments
As an important property that can be seen from the above
--For dummy arguments, keyword arguments cannot be set before positional arguments. --If the keyword argument is not set as an actual argument, the default value of the formal argument is applied. --In the actual argument, the keyword argument can be set by changing the order. -** Actual arguments can be called both as positional arguments and as keyword arguments **
Next, I will introduce only the keywords mentioned above. First, only the position, ** When defining a function, the argument before / can only be called as a positional argument ** On the other hand, only keywords ** When defining a function, the arguments after * can only be called with keyword arguments **
def func(a,*,b,c):
return a + b + c
def func2(a, /):
return a
print(func(1,b=2,c=3)) #b will result in an error if not called with a keyword argument
print(func2(1)) #a will result in an error if not called with a positional argument
First of all, the variable length position is ** an argument that can receive any number of position arguments **. ** When defining a function, add * before the argument to make it a positional argument (up to once per function) ** Variable-length keywords are ** arguments that can accept any number of keyword arguments **. ** When defining a function, add \ ** before the argument to make it a positional argument (up to once per function) **
def func(*a, **b):
print(a)
print(b)
func(1,2,3,b=4,c=5)
>>> (1, 2, 3)
{'b': 4, 'c': 5}
Recommended Posts