Python - arguments to functions

The parameters of the function are divided into:

1. Required parameters

2. Keyword arguments

3. Default parameters

 

 

Required parameter

The most basic feature is that the parameters we define in the function's parameter list must be passed.

def add(x,y):
    result = x +y
    return result

When we call this function, we must assign values ​​to x and y sub-tables. If neither of them is assigned or only one is assigned, an error will be reported.

 

When we define this function, we give xy in this parameter list, we can call x and y as formal parameters, or formal parameters for short.

There are formal parameters, corresponding to the actual parameters, referred to as actual parameters.

The actual parameter refers to the actual value that we pass into the function parameter list in the process of calling the function.

 

 

keyword arguments

 

Let's take the previous function as an example

def add(x,y):
    result = x +y
    return result

The required parameters are in an order when they are called. The first parameter passed in is the value of x, and the second passed in is the value of y.

So if we want to pass in the y value for the first time, and the x value for the second pass in, how to implement it?

Keyword parameters do not need to consider the order of function parameters, and the order of parameters can be arbitrarily specified to realize the call of parameters

c = add(y = 5,x = 2)

This will allow you to assign a value to y first. Of course, if assigning a parameter is still not enough, there are as many formal parameters as there are actual parameters.

 

 

default parameters

 

If we need a long parameter list of the function, and we need to use this function multiple times, and many parameters will be repeatedly entered, then we can set a default value for this function

def print_student_files(name,gender,age):
     print ( ' My name ' + name)
     print ( ' I am this year ' + str(age) + ' age ' )
     print ( ' I am ' + gender + ' birth ' )

 

This is a function that records the student's name, age, and gender (note that the age in the third line needs to be converted to a string, because strings cannot be added to int types)

>>> print_student_files( ' Annie ' , ' Female ' ,18 )
my name is anne
I am 18 years old
I am a girl

 

If there are 30 classmates, most of them are 18-year-old girls. Then we can use the default parameters:

def print_student_files(name,gender= ' female ' ,age= 18 ):
     print ( ' my name ' + name)
     print ( ' my year ' + str(age) + ' year ' )
     print ( ' my name ' + gender + ' raw ' )

In this way, we only need to enter the name

>>> print_student_files('曼妮')
My name is Manny
I am 18 years old
I am a girl

 

If there is a classmate whose age and gender are different from the default values, you need to pass the corresponding parameters:

>>> print_student_files( ' John ' , ' Male ' , 19 )
my name is john
I am 19 years old
i'm a boy

To summarize the rules for default parameters:

When defining, if you want a parameter to have a default value, add a '=' after the formal parameter and assign the default value to the formal parameter

If you don't set a default value for a parameter, you must pass an actual parameter.

 

Error prone:

Required parameters plus default parameters, followed by a required parameter

Error example 1:

def print_student_files(name,gender= ' female ' ,age):
     print ( ' my name ' + name)
     print ( ' my year ' + str(age) + ' age ' )
     print ( ' my name ' + gender + ' birth ' ) 
SyntaxError: non-default argument follows default argument

 Error: Cannot put non-default parameters after default parameters

 

Error example 2:

We want to change the second default parameter (age), the first default parameter (gender unchanged)

Suppose we convert each parameter to a string

>>> def print_student_files(name,gender= ' female ' ,age=18 ):
     print ( ' my name ' + str(name))
     print ( ' my year ' + str(age) + ' age ' )
     print ( ' i am ' + str(gender) + ' raw ' )

>>> print_student_files( ' Annie ' ,17 )
my name is anne
I am 18 years old
i am 17

Solution:

We passed age 17 as gender into the gender parameter. The solution is to use key parameters

>>> print_student_files( ' Annie ' ,age=17 )
my name is anne
I am 17 years old
I am a girl

 

Error example 3:

Call with default arguments and required arguments mixed together:

>>> print_student_files('安妮',gender='',17)
SyntaxError: positional argument follows keyword argument

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313214&siteId=291194637