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

Parameter types for Python functions

編輯:Python

Python The parameter type of the function

  • 1. Positional arguments
  • 2. Default parameter
  • 3. The key parameters
  • 4. Variable length parameter
    • 4.1) *parameter
    • 4.2) **parameter

Python When you define a function You don't have to specify The type of parameter , Argument types passed entirely by the caller and Python The interpreter's understanding and inference determine .
Next , This article will introduce the parameter types of the following four functions :

  • Positional arguments 、 Default parameter 、 Key parameters and variable length parameters .

1. Positional arguments

Positional arguments : Actual and formal parameters when calling a function The sequence must be strictly consistent , And arguments and formal parameters The quantity must be the same .

The following format :

def showArgs(a, b, c):
print(a, b, c)
show(1, 2, 3) # 1 2 3
show(1, 2) # TypeError: showArgs() missing 1 required positional argument: 'c'
show(1, 2, 3, 4) # TypeError: showArgs() takes from 1 to 3 positional arguments but 4 were given

2. Default parameter

Default parameter : When calling a parameter with a default value , You can not assign a value to a parameter that has a default value ( If there is , will Use the passed value ), That is, different from the above position parameters , Parameter passing can be missing , But you can't pass on more .

How to use it : namely , stay When defining a function def demo(a=1, b=2) A default value is given when filling in parameters .

Let's take an example :

def showArgs(a, b=3, c=2):
print(a, b, c)
showArgs(1, 2, 3) # 1 2 3
showArgs(1, 2) # 1 2 2
showArgs(1, 2, 3, 4) # TypeError: showArgs() takes from 1 to 3 positional arguments but 4 were given

matters needing attention :

  • The default value parameter must appear in the function parameter list Rightmost end , The right side of any default value parameter cannot have non default value parameters
  • Assignment of default value parameter It will only be interpreted once when the function is defined
    ( Therefore, the default value parameter should preferably be of immutable type )

3. The key parameters

Use , The key parameters , The order of arguments and formal parameters can be different , However, if the parameter has no default value , The number of parameters should also be consistent .

How to use it : stay When you call a function demo(a=1, b=2) To specify the value of the parameter .

Let's take an example :

def showArgs(a, b, c):
print(a, b, c)
showArgs(1, 2, c=3) # 1 2 3
showArgs(a=1, c=22, b=33) # 1 33 22

matters needing attention :

  • Key parameters cannot be followed by position parameters

Wrong situation :

showArgs(1, 22, b=33)

It will report this error :TypeError: showArgs() got multiple values for argument 'b', Because though b Key parameters are used , But the first two are positional parameters , Through position parameters ,b It has been assigned

showArgs(a=11, c=222, 333)

It will report this error :SyntaxError: positional argument follows keyword argument, Key parameters cannot be followed by positional parameters


4. Variable length parameter

Variable length parameter : That is, the parameter length is not fixed
It has the following two forms :

  • *parameter: Receive multiple Location parameter And put it in Tuples in
  • **parameter: Receive multiple The key parameters And put it in Dictionaries in

Among them parameter You can customize the name you want

4.1) *parameter

Must receive Positional arguments !!!
Its usage is as follows :

def showArgs(*args):
print(args)
showArgs(1, 2, 3) # (1, 2, 3)
showArgs(7, 8) # (7, 8)

4.2) **parameter

Must receive The key parameters !!!
Its usage is as follows :

def showArgs(**args):
print(args)
showArgs(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3}
showArgs(a=7, b=8) # {'a': 7, 'b': 8}


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