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

Detailed explanation of Python function parameters

編輯:Python

Python A detailed explanation of the parameters of the function

1、Python Type of function parameter for

  • Must pass parameters : Parameters that must be passed in when calling a function , When defining a function, only the parameter name is defined
  • Key parameters : When it is passed in, it is passed in the form of parameter name value pairs of the function
  • Default parameters : When defining a function, set a value for the parameter by default , The call is processed with default values without transferring parameters
  • Indefinite length parameter : With * Embellish or ** Modified parameters ;* The decorated parameter is a tuple (tuple),** The decorated parameter must be a dictionary (dict), Usually writing *args perhaps **args

2、Python The required parameters of

  • The number of parameters passed in must be consistent with the number of formal parameters
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a))
print(b, type(b))
# demo01(1) #TypeError missing 1 required positional argument: 'b' An unknown parameter is missing ,demo01 Must pass 2 Parameters 
# demo01(1, 2, 3) #TypeError demo01() takes 2 positional arguments but 3 were given, demo01 Function has 2 Position parameters , But I got it 3 individual ;
# Must pass parameters : Parameters that must be passed in when calling a function , When defining a function, only the parameter name is defined 
# The number of parameters passed in must be consistent with the number of formal parameters 
demo01(1, 2) # yes
demo01(1, [1, 2]) # yes
demo01([2, 3], (1, 2)) # yes
demo01(1, {2, 3, 4}) # yes
demo01(2, {"code":'1001', "name":"zhang", "age":18}) # yes

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

3、 Key parameters

  • Transfer parameters by associating parameter names with parameter values , Mode of key value pair , The parameter name is key .
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo01(a,b):
print(a, type(a), end="__")
print(b, type(b))
# The parameter before the parameter position in and out of the first keyword is better than that of the selected keyword ; As in the following example , The first key parameter variable is a,a In the second position of the function definition , Then the parameters before the second position must be passed in the form of keyword parameters .
# demo01(1, a=2) # demo01() got multiple values for argument 'a'
demo01(a=1, b=2) # yes
demo01(1, b=2) # yes
demo01(b=1, a=2) # yes
demo01(b=1, a=[1, 2]) # yes
demo01(b=[2, 3], a=(1, 2)) # yes
demo01(b=1, a={2, 3, 4}) # yes
demo01(b=2, a={"code":'1001', "name":"zhang", "age":18}) # yes

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

4、 Default parameters

  • Call the function without passing in parameters an The default value is processed , It is processed according to the passed in parameter value
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
## Default parameters 
def demo02(City = "LongNan"):
print("City Is the default parameter , The default value is : China , The current value is :", City)
# No parameter passed in 
demo02()
# Pass in the parameter 
demo02("Beijing")

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

5、 Indefinite length parameter

  • The passed in parameter will generate a variable of tuple type for internal use of the function
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
## Indefinite length parameter 
def demo03(*args):
print(args,type(args))
# The passed in parameter will generate a variable of tuple type for internal use of the function 
demo03(1)
demo03("code")
demo03(1,"code")

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

  • The number of passed in parameters cannot be less than the number of required parameters
  • *args The shape parameter at the back , Parameters must be transferred with keyword parameters
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
# *args The shape parameter at the back , Parameters must be transferred with keyword parameters ,
def demo04(a, b, *args,c):
print("a The parameter value passed in is :{},b The parameter value passed in is :{}, args The parameter passed in is :{}, c The access parameters are :{}".format(a, b, args,c))
# The number of passed in parameters cannot be less than the number of required parameters ,a,b,c Three parameters are required 
# demo04(1, 2) # TypeError
# demo04(1, 2, 3) # TypeError
demo04(1, 2, c=3)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

  • *args Previous parameters can only be transferred by position , You cannot transfer parameters by keyword
  • When the number of parameters passed in is more than the number of mandatory parameters , First, assign values to the mandatory parameters according to their positions , Then generate a tuple from the remaining parameters and pass it to args

  • ** Decorated parameters must be passed as keyword parameters ,Python The interpreter will generate a dictionary of the passed in keywords and their values for internal use
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
def demo05(**kwargs):
print("kwargs The parameter passed in is :{}".format(kwargs),type(kwargs))
kwargs = {"code":'1002', "name":"zhang"}
# demo05(kwargs) # TypeError
# ** Decorated parameters must be passed as keyword parameters ,Python The interpreter will generate a dictionary of the passed in keywords and their values for internal use 
demo05(**kwargs) # kwargs The parameter passed in is :{'code': '1002', 'name': 'zhang'} <class 'dict'>
demo05(code='1002',name="zhang") # kwargs The parameter passed in is :{'code': '1002', 'name': 'zhang'} <class 'dict'>

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

  • ** The decorated parameter must be the last
# coding:utf-8
# Time:2022/6/27 20:04
# Author:YangXiaoPeng
# ** The decorated parameter must be the last 
""" # SyntaxError: invalid syntax def demo06(a, b, *args, c, **kwargs, d): pass """
def demo06(a, b, *args, c, **kwargs):
print("a The parameter value passed in is :{},b The parameter value passed in is :{}, args The parameter passed in is :{}".format(a, b, args),end='')
print(",c The parameter passed in is :{},kwargs The parameter passed in is :{}".format(c,kwargs))
demo06(1, 2, 3, 4, 5, c=3, code='1002', name="zhang", d=3)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.


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