Function parameters - function basis

And a parameter argument Dian

1.1 parameter
argument function defined in the definition phase bracket, called the formal parameters, parameter referred to, is essentially the variable name.

def func(x, y):
    print(x)
    print(y)

1.2 argument

Passed in the function call stage parentheses parameters, known as actual parameters, referred to the argument, in essence, is the value of the variable.

func(1,2)


Dian two positional parameters

2.1 positional parameters
in the function definition phase in accordance with the formal parameters defined sequentially from left to right, called the position parameter.

def func(x, y):
    print(x)
    print(y)

Features: parameter defined according to the position, must be transmitted value, not a plurality, at least one too.

2.2 the position arguments
in the function call stage, left to right, sequentially defined argument, called argument position.

func(1,2)

Features: position according to parameter values ​​corresponding to sequentially pass.


Wed and keyword arguments

When calling the function, in the form of key = value passed as a parameter to the specified value, referred to as keyword arguments.

func(y=2, x=1)

Features: You can break the limit position, but still for the specified parameter assignment.

Note:
 1. You can mix position arguments and keyword arguments, but the location argument must be the keyword arguments left.
 2. The position argument can mix and Keyword argument, but can not repeat the assignment for a parameter.

func(x, y=2)
func(y=2, x)  # SyntaxError: positional argument follows keyword argument
func(x, x=1)  # NameError: name 'x' is not defined


Four Dian default parameter

During the definition phase, it has been assigned.

def func(x, y=10):
    print(x)
    print(y)
    
func(2)

Features: During the definition phase has already been assigned, which means you can not assign a value when calling.

note:

 1. The position of the parameter in the default parameter must be left.
 2. The default parameter values assigned only once in the definition phase, which means that the default value of the parameter in the function definition phase has been fixed.

m = 10


def foo(x=m):
    print(x)


m = 111
foo()  # 10

 The default value of 1. The parameter should normally be immutable.
 2. Variable * type usually used to break up the conversion method, a function of the corresponding values obtained by the participants shape * method.

# 演示形参是可变类型
def register(name, hobby, hobby_list=[]):
    hobby_list.append(hobby)
    print(f"{name} prefer {hobby}'")
    print(f"{name} prefer {hobby_list}")


register('nash', 'read')
register('tank', 'zuipao')
register('jason', 'piao')

nash prefer read' nash prefer ['read'] tank prefer zuipao' tank prefer ['read', 'zuipao'] jason prefer piao' jason prefer ['read', 'zuipao', 'piao']

# 修改形参是可变类型代码
def register(name, hobby, hobby_list=None):
    if hobby_list is None:
        hobby_list = []
    hobby_list.append(hobby)
    print(f"{name} prefer {hobby}'")
    print(f"{name} prefer {hobby_list}")


register('nash', 'read')
register('tank', 'zuipao')
register('jason', 'piao')

nash prefer read' nash prefer ['read'] tank prefer zuipao' tank prefer ['zuipao'] jason prefer piao' jason prefer ['piao']


Dian five summary

Application of arguments: depending on personal habits
application of formal parameters:

 1. Most of the same value or call, the parameters should be defined as a location parameter
 2. In most cases the value of the same call, the parameters should be defined as the default parameter

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374784.html