程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics - positional and keyword parameters

編輯:Python

1、 The type of arguments to the function

  • Shape parameter : Parameters when defining a function , Such as defining functions def func(a,b) Parameters of a,b It's a parameter .
  • Positional arguments : The number and location of the actual parameters passed in when calling a function must be the same as when defining the function .
  • Key parameters : When calling a function, you use key value pairs ,key=values. When transferring mixed parameters, the keyword parameter must be after the positional parameter .
  • Default parameters : When defining a function , Specify a default value for the formal parameter directly . When calling a function, if no arguments are passed in , The default parameter is taken . If you pass in an argument , Then take the actual parameter .
  • Actual parameters : The value when the function is called , Such as calling a function func(2,3) Parameters of 2,3 Is the argument .
  •  *args and **kwargs Parameters

2、 Positional arguments

2.1 The position parameter is called normally

# Custom function
def func(a, b):
print(a ** b)
# Normal call
func(2,3)
------------------------------------------------------------------
Running results :
8

  2 ** 3 =2*2*2=8

2.2  The number of arguments and parameters must be the same

If the number of arguments and formal parameters are inconsistent, a syntax error will be reported , Such as calling func Function only passes in the result of an argument

# Custom function
def func(a, b):
print(a ** b)
# The number of arguments and parameters must be the same
func(2)
----------------------------------------------------------------------
Running results :
TypeError: func() missing 1 required positional argument: 'b'

Empathy , Multiple incoming parameters will also be reported  TypeError

2.3  Argument and parameter positions must be the same

# Custom function
def func(a, b):
print(a ** b)
# Argument and parameter positions must be the same
func(2, 3)
func(3, 2)
-----------------------------------------------------------------------
Running results :
8
9

2 ** 3 = 2*2*2 = 8

3 ** 2 = 3*3 = 9

The results show that , The positions of formal and actual parameters must be consistent ,func(2,3) and func(3,2) It's different .

3、 Key parameters

3.1 Normal invocation of keyword parameters

# Custom function
def func(a, b):
print(a ** b)
# Normal call
func(a=2, b=3)
func(b=3,a=2)
----------------------------------------------------------
Running results :
8
8

  We can easily draw a conclusion from the above example , It is no longer necessary to be exactly the same as the position of the parameter , Just write the parameter name correctly .

3.2  Mixed use of keyword parameters and positional parameters

# Custom function
def func(a, b):
print(a ** b)
func(2, b=3)
-------------------------------------------------------------------------
Running results :
8
# Custom function
def func(a, b):
print(a ** b)
func(a=2, 3)
--------------------------------------------------------------------------------
Running results :
SyntaxError: positional argument follows keyword argument

  We can easily draw a conclusion from the above example , Keyword parameters can be mixed with positional parameters , But the keyword parameter must be after the positional parameter , Otherwise you will report a syntax error SyntaxError 

3、 Default parameters

# Custom function , Parameters with default values must follow all parameters without default values
def func(a, b=3):
print(a ** b)
# Default parameters
func(2)
func(2,1)
-----------------------------------------------------------------------
Running results :
8
2

The results show that , Corresponding to the default parameter , When calling a function, if no arguments are passed in , The default parameter is taken . If you pass in an argument , Then take the actual parameter .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved