# [python learning] function parameters of python

[Qi Ji Yu position] in Chinese History:
After Yu's death, his son Qi ascended the throne and became the first person in China to change from "Zen concession system" to "hereditary system".
-Source: full history APP

Today we'll talk about python's function parameters. If necessary, you can also go directly to my github to view all notes:

https://github.com/JackKoLing/python_notes_with_ten_days As the saying goes, "a good memory is better than a bad pen". If you write more and remember more, you will never be wrong. More insistence for no reason and less utilitarian pursuit. For environment configuration, please refer to the following two articles:

## 1 simple example of a function

• Polymorphism: a function implements multiple types of operations
```def f1(x):
print(x)
f1(3)
f1("Jackko")
>>>
3
Jackko
```
```def f2(x, y):
print(x+y)
f2(3, 4)
f2("hello ", "world")
>>>
7
hello world
```
```m=3
n=4
def f3(x, y):
x -= 1
print(x, y)
f3(m, n)
print(m, n) # Variables are immutable objects
>>>
2 4
3 4
```
```l1 = [1, 2, 3]
def f4(x):
x.pop()
print(x)
f4(l1) # Lists are mutable objects
>>>
[1, 2]
```
```l1 = [1, 2, 3]
def f5(x):
x.pop()
print(x)
f5(l1[:]) # Instead of passing l1 over, get a slice of a new object
print(l1)
>>>
[1, 2]
[1, 2, 3]
```

## 2 parameter matching model

• By default, parameters are passed through their positions, from left to right. Therefore, as many parameters as function header parameters must be accurately passed. This mechanism can also be changed through keyword parameters, default parameters or parameter containers
• Position parameters: accurate matching from left to right
• Keyword parameter: when calling a function, use the syntax of "name=value" to match the parameter name
• Default parameter: when defining a function, use the syntax of "name=value" to directly give a value to the variable, so that the passed in value can be less than the number of parameters
• Variable parameters: when defining a function, the parameters starting with * can be used to collect any number of location-based or keyword based parameters
• Unpacking of variable parameters: when calling a function, the parameters starting with * are used to break up the parameter set, so as to pass any number of location-based or keyword based parameters
```# Position parameters: accurate matching from left to right
m = 3
n = 4
def f6(x, y):
print(x, y)
f6(m, n)
# Keyword parameter, which is used when calling
f6(y=m, x=n)
>>>
3 4
4 3
```

Put all location parameters first, and then all keyword parameters

```# When mixing location parameters and keyword parameters, you must put all location parameters first and then all keyword parameters from left to right
o = 7
def f7(x, y, z):
print(x, y, z)
f7(m, z=o, y=n) # f7(z=o, y=n, m) is a syntax error
>>>
3 4 7
```

The default parameters are placed later

```# Default parameter: used when defining a function. It is also required that the default parameter be placed later
def f8(x, y, z=9):
print(x, y, z)
f8(m, n, o)
f8(m, n) # If the third parameter is not given, the default parameter is used
>>>
3 4 7
3 4 9
```

A * is to collect location parameters and return tuples

```# Variable parameters: used when defining functions, similar to collecting parameters. A * is to collect location parameters and return tuples
def f9(*x):
print(x)
f9(m)
f9(m, n, o)
f9(m, n, 9)
>>>
(3,)
(3, 4, 7)
(3, 4, 9)
```

The two * are the collection keyword parameters and return the dictionary type

```# The two * of variable parameters are collection keyword parameters and return dictionary type
def f10(**x):
print(x)
f10(x=1, y=2, z=9)
>>>
{'x': 1, 'y': 2, 'z': 9}
```
```# Mix position parameters and variable parameters
def f11(x, *y):
print(x, y)
f11(m, n, o)
>>>
3 (4, 7)
```
```def f12(x, y=10, *z):
print(x)
print(y)
print(z)
f12(m, n, o)
f12(m) # If z is not passed, an empty tuple is returned
>>>
3
4
(7,)
3
10
()
```

Again: one is to collect location parameters and return tuples; Two are to collect keyword parameters and return dictionary types

```def f13(*x, **y):
print(x)
print(y)
f13(m, n, o, i=3, j=6)
>>>
(3, 4, 7)
{'i': 3, 'j': 6}
```

Variable parameter unpacking: used when calling a function

```# Variable parameter unpacking: used when calling a function, just opposite to variable parameters. This is a decomposition parameter
l1 = ['Sun', 'Mon', 'Tus']
def f14(x, y, z):
print(x, y, z)
f14(*l1) # The number of elements needs to match the function parameters
l2 = ['a', 'b']
f14(m, *l2)
>>>
Sun Mon Tus
3 a b
```
```def f15(x, *y):
print(x)
print(y)
f15(m, *l2)
>>>
3
('a', 'b')
```
```d1 = {'k1':1, 'k2':2, 'k3':3}
def f16(x, *y, **z):
print(x)
print(y)
print(z)
f16(m, *l2, **d1)
>>>
3
('a', 'b')
{'k1': 1, 'k2': 2, 'k3': 3}
```
```f16(m, n, o, **d1)
>>>
3
(4, 7)
{'k1': 1, 'k2': 2, 'k3': 3}
```
```f16(m, n, o, k1='v1', k2='v2') # In any case, it is the location parameter, which corresponds to the keyword parameter
>>>
3
(4, 7)
{'k1': 'v1', 'k2': 'v2'}
```

## 3 anonymous function lambda

```lambda args: expression # args is the parameter and expression is the statement
```
• lambda is an expression, not a statement
• A lambda statement must be a valid expression. It is a single expression, not a code block
• The primary purpose of lambda is to specify a short callback function, that is, the function function is very simple. You can try to use lambda
• lambda will return a function instead of assigning a function to a variable name
• Lambda also supports the use of default parameters, such as f=(lambda x,y,z=10: x+y+z)
```f = lambda x, y : x+y # Execute x+y
f(3, 4)
>>>
7
```
```l3 = [ (lambda x: x*2), (lambda y: y*3)]
for i in l3:
print(i(4)) # Function call. Note that l3 is a list, so i represents two lambda expressions
>>>
8
12
```

[statement]: learning notes are based on personal arrangement of various learning resources on the Internet.

The above is the content of this issue. The next issue introduces the functional programming of python.

My name is Xiao Bao, a computer vision enthusiast, learner and follower. Welcome to study with me. Tags: Python Deep Learning

Posted on Tue, 26 Oct 2021 08:22:18 -0400 by jlh3590