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

Other type parameters of Python functions

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .

Catalog

      • One 、 background
      • Two 、 Positional arguments
      • 3、 ... and 、 Key parameters
      • Four 、 Indefinite length parameter
      • 5、 ... and 、 Reference resources
      • 6、 ... and 、 summary


One 、 background

In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function positional parameters for 、 Key parameters , Related contents of variable length parameters .


Two 、 Positional arguments

When you call a function , The order of arguments and formal parameters must be strictly consistent , And the number of arguments and formal parameters must be the same .
example : Run the following program , Analyze the running results .

def printme(a, b, c):
print (a, b, c)
printme(1, 2, 3)
printme(1, 2)

give the result as follows .


3、 ... and 、 Key parameters

Keyword parameter refers to the parameter passing method when calling a function , Is a way to pass values by parameter name . The use of keyword parameters allows the order of parameters in function calls to be inconsistent with the definition ,Python The interpreter can match parameter values with parameter names .

example : Key parameters .

def printinfo(name, age):
print (" name : ", name)
print (" Age : ", age)
return
# call printinfo function 
printinfo(age=50, name="runoob")

Four 、 Indefinite length parameter

Usually when defining a function , If you want a function to be able to handle more parameters than it was defined , In this case, the variable length parameter can be used in the function .

  • *args Used to receive any number of arguments and put them in a tuple
  • **kwargs It is used to receive multiple arguments in the form of explicit assignment similar to keyword parameters and put them into the dictionary
def Function name ([ Parameter list ,] *args, **kwargs):
The body of the function

example : Indefinite length parameter .

def f(a, b, *args, **kwargs):
print(a)
print(b)
print(args)
print(kwargs)
f(1, 2, 3, 4, 5, x = 6, z = 7)

give the result as follows .


5、 ... and 、 Reference resources

1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial


6、 ... and 、 summary

The above is about python Function positional parameters for 、 Key parameters , Related contents of variable length parameters , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .


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