当前位置:网站首页>3、 The parameters of the function

3、 The parameters of the function

2020-11-08 13:24:47 YuMi-sharing

  1. Formal parameter and actual parameter
  • Formal parameters (parameter), It's called formal parameter for short , The parameters in brackets in the function definition process ;
  • The actual parameter (argument), It's called argument for short , The actual parameters in the call process are referred to .
>>> def  my_function(param1, param2):  # param1, param2 Is a formal parameter 
	return param1** 2 + param2 ** 2

>>> my_function(3, 4)  # 3,4 For reference 
  1. The specific type of the parameter
    Function parameters can be classified into the following categories :
  • Positional arguments
    Fixed position parameters , The parameters of the function defined above are positional parameters

  • Key parameters
    Position parameter one is easy to be confused , Therefore, it is very useful to introduce the keyword parameter of this mapping type when calling a function

>>> def learn(teacher, student, course):
	print(teacher + " teach " + student + " learn " + course)

	
>>> learn(" Teacher wang ", " Xiao Ming ", " mathematics ")
 Mr. Wang teaches Xiaoming math 
>>> learn(" Xiao Ming ", " Teacher wang ", " mathematics ")
 Xiao Ming teaches Mr. Wang math 
>>> learn(student = " Xiao Ming ", course = " mathematics ", teacher = " Teacher wang ")
 Mr. Wang teaches Xiaoming math 
>>> learn(" Teacher wang ", student = " Xiao Ming ", course = " mathematics ")
 Mr. Wang teaches Xiaoming math 
>>> learn(course = " mathematics ",  " Xiao Ming ", teacher = " Teacher wang ") #  The positional parameter must precede the keyword parameter ,  Or you'll make a mistake 
SyntaxError: positional argument follows keyword argument
>>> 
  • Default parameters
    python Function allows you to specify default values for parameters , If no argument is passed in the function call, the default value is used .
>>> def learn(teacher = " Miss li ", student = " Xiaohong ", course = " English "):
	print(teacher + " teach " + student + " learn " + course)

	
>>> learn()
 Miss Li teaches Little Red Mansions English 
>>> learn(" Xiao Ming ", " Teacher wang ", " mathematics ")
 Xiao Ming teaches Mr. Wang math 
>>> learn(student = " Xiao Ming ", course = " mathematics ", teacher = " Teacher wang ")
 Mr. Wang teaches Xiaoming math 
>>> learn(student = " Xiao Ming ")
 Mr. Li teaches Xiao Ming to learn English 
>>> def learn(teacher = " Miss li ", student = " Xiaohong ", course): #  When defining a function, if there are positional parameters , Before the default parameter , Otherwise, it will report a mistake 
	print(teacher + " teach " + student + " learn " + course)
	
SyntaxError: non-default argument follows default argument
  • Variable parameters
    The reason for using variable parameters ( It's also called collecting parameters ), It's because callers of functions sometimes don't know how many arguments to pass in .
    The most typical is that we are very familiar with print() function
>>> print(1,2 ,3)
1 2 3
>>> print(1,2,3,4,5)
1 2 3 4 5

print() The definition of function is to use variable parameters :

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments. python3.8 Official documents

There are two ways of syntax for variable parameters :* perhaps **

>>> def output_alpha(*letter):
	for i in letter:
		print(i)

		
>>> output_alpha('a', 'b', 'c')
a
b
c

Add (*) The variable parameter of is actually to package all the parameters into a tuple .

>>> def output_alpha(*letter):
	print(type(letter))

	
>>> output_alpha('a', 'b')
<class 'tuple'>

Note that if you need to specify other parameters after the variable parameter , So when it's called ( There are no requirements for function definition , Positional arguments 、 The default parameters can be ) You should use keyword parameters , Otherwise python All parameters will be included in the variable parameters .
On the parameter setting of the function ,print() Function is our model , Take it out again :

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

If you want to set variable parameters , So when passing in the arguments, the emphasis must be on the variable parameters , So you can set the other parameters as default parameters , This is not too convenient and flexible .

Another form of variable parameter is to use (**), The difference is to package all the parameters into a dictionary .

>>> def student(**info):
	print(info)

>>> student(Amy=1, Tom=2, Bob=3)
{'Amy': 1, 'Tom': 2, 'Bob': 3}

版权声明
本文为[YuMi-sharing]所创,转载请带上原文链接,感谢