Python function tutorial: required parameters and default parameters

Required parameters

Positional parameters must be passed into the function in the correct order. The number when called must be the same as when declared.

def power(x, n):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

print(pow(5,3))  #125

The modified power(x, n) function has two parameters: x and n. These two parameters are positional parameters. When the function is called, the two values ​​passed in are assigned to the parameters x and n in order of position.

Default parameters

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def power(x, n=2):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

print(power(5)) #25
print(pow(3,3)) #27

Default parameters can simplify the calling of functions. When setting default parameters, you must select the first parameter, and the default parameter is the last.

When there are multiple default parameters, you can provide the default parameters in order when calling:

def enroll(name, gender, age=6, city='Beijing'):
    print('name:', name)
    print('gender:', gender)
    print('age:', age)
    print('city:', city)

enroll('Bob', 'M', 7)

Output:

name: Bob
gender: M
age: 7
city: Beijing

It is also possible to provide some default parameters out of order. When part of the default parameters are provided out of order, the parameter name needs to be written:

enroll('Adam', 'M', city='Tianjin')

Output:

name: Adam
gender: M
age: 6
city: Tianjin

This means that the city parameter uses the value passed in, and other default parameters continue to use the default values.

Default parameters must point to immutable objects

Define the function:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def add_end(L=[]):
    L.append('END')
    return L

When calling normally:

L1 = add_end([1, 2, 3])
print(L1) #[1, 2, 3, 'END']

L2 = add_end(['x', 'y', 'z'])
print(L2) #['x', 'y', 'z', 'END']

But if you use the default call:

L1 = add_end()
print(L1) #['END']
L2 = add_end()
print(L2) #['END', 'END']
L3 = add_end()
print(L3) #['END', 'END', 'END']

The default parameter is [], but the function seems to "remember" the list after adding'END' last time every time.

The reason is explained as follows:

When the Python function is defined, the value of the default parameter L is calculated, that is [], because the default parameter L is also a variable, which points to the object [], every time the function is called, if the content of L is changed, then The next time it is called, the content of the default parameter will change, and it is no longer the [] when the function is defined.

To modify the above example, we can use the None immutable object to achieve:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
def add_end(L=None):
    if L is None:
        L = []
    L.append('END')
    return L

L1 = add_end()
print(L1) #['END']
L2 = add_end()
print(L2) #['END']
L3 = add_end()
print(L3) #['END']

Because once an immutable object is created, the data inside the object cannot be modified, which reduces errors caused by modifying the data. In addition, because the object is unchanged, simultaneous reading of the object in a multitasking environment does not require locking, and there is no problem with simultaneous reading. When we are writing a program, if we can design an immutable object, then try to design it as an immutable object.

Guess you like

Origin blog.csdn.net/qdPython/article/details/112573371