python functions and lambda expressions study notes

1. python function

Unlike other languages, python support function to return multiple values
as a function of providing documentation: help (function name) or function name .__ doc__

def str_max(str1, str2):
    '''
    比较两个字符串的大小
    '''
    str = str1 if str1 > str2 else str2
    return str
help(str_max)
print(str_max.__doc__)
Help on built-in function len in module builtins:
len(obj, /)
    Return the number of items in a container.

out[2]:'Return the number of items in a container.'

2. python transfer function values ​​and reference (address) is transmitted

  1. Value is passed: argument type is suitable for immutable type (string, number, component);
  2. Pass a reference: the type of argument applies to immutable type (list, dictionary);
  3. And transmitting the difference value passed by reference: the value of the function parameter is transmitted, if the parameter value changes, does not affect the value of the argument; and the function parameters are passed by reference, changing the value of the parameter, the value of an argument It will change together.
def demo(obj)
    obj += obj
    print("形参值为:",obj)
print("---值传递---")
a = "孙悟空"
print("a的值为:",a)
demo(a)
print("实参值为:",a)
print("---引用传递---")
a = [1, 2, 3]
print("a的值为:",a)
demo(a)
print("实参值为:",a)
运行结果为:
-------值传递-----
a的值为: 孙悟空
形参值为: 孙悟空孙悟空
实参值为: 孙悟空
-----引用传递-----
a的值为: [1, 2, 3]
形参值为: [1, 2, 3, 1, 2, 3]
实参值为: [1, 2, 3, 1, 2, 3]

3. python function parameter passing mechanism

  1. Value is passed
    so-called value is passed, in fact, a copy of the actual parameter values (replica) passed to the function, and the parameter itself will not be affected.
  2. Pass a reference
    if the actual parameter data type is mutable objects (list, dictionary), the transfer function parameters embodiment reference will employ transfer mode. Reference to the underlying implementation of transmission, is still used in the way passed by value.

Guess you like

Origin www.cnblogs.com/xiaobaizzz/p/12210094.html