About function arguments (python)


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))

STEP1: About actual arguments and formal arguments

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
  return a

print(foo(1)) #Actual argument

STEP2: Properties of dummy arguments

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:]

lst = [1, 2, 3, 4]
 >>> [2, 3, 4]
 >>> [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.

STEP3: Types of formal parameters

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)
print(foo2(1,5)) #Positional argument (a=1,b=5)
print(foo2(1,b=5)) #Positional and keyword arguments
print(foo2(a=1,b=5)) #Keyword arguments
print(foo2(b=2)) #error
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 **

STEP4: Position only, keyword only

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

STEP5: Variable length position, variable length keyword

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):

 >>> (1, 2, 3)
     {'b': 4, 'c': 5}

Recommended Posts

About function arguments (python)
Python: About function arguments
python function ①
[Python] function
About python beginner's memorandum function
About the enumerate function (python)
python function ②
Included notation in Python function arguments
[Python] Function arguments * (star) and ** (double star)
About python slices
About python comprehension
python enumerate function
About Python tqdm.
About python yield
About python, class
Python> function> Closure
[Python] Generator function
About python inheritance
About python, range ()
About python decorators
Python> function> Inner function
About python reference
About Python decorators
[Python] About multi-process
Python function decorator
About the arguments of the setup function of PyCaret
Combine python function arguments into one variable
About Python for loops
Summary about Python scraping
Automatically register function arguments to argparse in Python
Function execution time (Python)
Example of taking Python> function> * args as arguments
[Python] Memo about functions
Summary about Python3 + OpenCV3
About Python, for ~ (range)
Python function argument summary
About Python3 character code
[Python] Memo about errors
About Python development environment
Python print function (sequel)
About the Unfold function
Python, about exception handling
Time floor function (Python)
About Python Pyramid traversal
Summary of Python arguments
About Python3 ... (Ellipsis object)
[Python] Chapter 01-01 About Python (First Python)
[Python] About standard input
About __all__ in python
Execute Python function from Powershell (how to pass arguments)
[Python] Find out about pip
About django custom filter arguments
About Fabric's support for Python 3
Create a function in Python
Use callback function in Python
About python objects and classes
About Python variables and objects
[python] Value of function object (?)
About the Python module venv
ntile (decile) function in python
Think about architecture in python