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

Python function explanation

編輯:Python

Definition and use

# coding:utf-8
if __name__ == '__main__':
# Definition and use 
''' Function by def+ Function name ( No arguments | Parameters .......) Definition By function name ( No arguments | Parameters ......) call '''
def talk(name):
print(name)
talk('func') # func

Parameter type

# coding:utf-8
if __name__ == '__main__':
''' example 1: Required parameters , If you do not fill in, you will report an error . Statement :def Function name ( Parameters ) example 2: Not a required parameter , If it is not filled in, the default value will be used . Statement :def Function name ( Parameters = The default value is ) example 3: Variable tuple parameter , When the function will be called , Unmatched parameter and parameter transfer format is not parameter name = All parameters of the parameter value form tuple. Statement :def Function name (* Parameter name ) example 4: Variable dictionary parameters , When the function will be called , Unmatched parameter and parameter transfer format is parameter name = All parameter names of parameter values , Parameter value formation dict. Statement :def Function name (** Parameter name ) The recommended order of declaration of function parameters is :def Function name ( Required parameters ......, Not a required parameter ......, Variable tuple parameter , Variable dictionary parameters ) '''
# example 1
def talk(name):
print(name)
talk('func') # func
# talk() Error Required parameters must be filled in 
# example 2
def talk(name='func'):
print(name)
talk() # func
talk('ok') # ok
# example 3
def talk(*args):
print(args)
talk(1, 2, 3) # (1, 2, 3)
# example 4
def look(**kwargs):
print(kwargs)
look(name='xie', sex='man') # {'name': 'xie', 'sex': 'man'}

Parameter matching order

# coding:utf-8
if __name__ == '__main__':
''' example 1: The parameter name... Does not appear = Parameter value transfer form , Parameters match from left to right example 2: When the parameter name appears = After the parameter transfer form of the parameter value , Match by parameter name , Parameter names are also required for subsequent parameters = Parameter transfer form of parameter value , Otherwise, the report will be wrong example 3: Parameter name used before variable tuple parameter = Variable tuple parameters cannot be assigned due to the parameter passing form of parameter value , Report errors example 4: Parameter name used before variable dictionary parameter = The parameter transfer form of parameter value does not affect the assignment of variable dictionary parameters '''
# example 1
def see(a, b=0, c=1):
print(f'a is {
a},b is {
b} ,c is {
c}')
see(1, 2, 3) # a is 1,b is 2 ,c is 3
# example 2
see(c=3, b=2, a=1) # a is 1,b is 2 ,c is 3
# see(a=1, 2, 3) Error The following parameter transfer format should also use the parameter name = The form of parameter value 
# example 3
def ps(a, b=0, c=1, *args):
print(f'a is {
a},b is {
b} ,c is {
c},args is {
args}')
ps(1, 2, 3, 4, 5) # a is 1,b is 2 ,c is 3,args is (4, 5)
# ps(1, b=2, c=3, 4, 5) Error Parameter name used before variable tuple parameter = The parameter value cannot be assigned to a variable tuple parameter due to its parameter passing form 
# example 4
def ks(a, b=0, c=1, **kwargs):
print(f'a is {
a},b is {
b} ,c is {
c},kwargs is {
kwargs}')
ks(1, 2, 3, name='xie', sex='man') # a is 1,b is 2 ,c is 3,kwargs is {'name': 'xie', 'sex': 'man'}
ks(a=1, b=2, c=3, name='xie', sex='man')

Tuple to variable tuple parameter

# coding:utf-8
if __name__ == '__main__':
''' adopt *tuple Change tuple to variable tuple parameter *tuple It can pass its own elements into the function as parameters in turn tp( Parameters ......,*(1,2......)) Equivalent to tp( Parameters ......,1,2......) '''
def tp(a, *args):
print(f'a is {
a},args is {
args}')
tp(1, 2, 3) # a is 1,args is (2, 3)
tp(1, (2, 3)) # a is 1,args is ((2, 3),)
tp(1, *(2, 3)) # a is 1,args is (2, 3)
def tp(a, b):
print(a, b)
tp(*(1, 2)) # 1 2

Dictionary to variable dictionary parameters

# coding:utf-8
if __name__ == '__main__':
''' adopt *dict Change a word to a variable dictionary parameter **dict Can translate dictionary elements into key=>value Pass in functions as parameters in turn dp( Parameters ......,**{key1:value1,key2:value2......}) Equivalent to dp( Parameters ......,key1=value1,key2=value2......) '''
def dp(a, **kwargs):
print(f'a is {
a},kwargs is {
kwargs}')
dp(1, name='xie', sex='man') # a is 1,kwargs is {'name': 'xie', 'sex': 'man'}
dp(1, **{
'name': 'xie', 'sex': 'man'}) # a is 1,kwargs is {'name': 'xie', 'sex': 'man'}
def dp(name, sex):
print(f'name is {
name}, sex is {
sex}')
dp(**{
'name': 'xie', 'sex': 'man'})

Parameter type conventions

# coding:utf-8
if __name__ == '__main__':
''' Through parameters : type (python3.7 Only later will there be ) To tell others the type of parameter , To show ,python It's not going to verify '''
def lsp(name: bool):
print(name)
lsp('ok') # ok No errors are reported 

lamdba function

# coding:utf-8
if __name__ == '__main__':
#lambda function 
''' Definition lambda Parameters 1, Parameters 2...... : Simple logic ( No need to add return, add to return False report ) call take lambda Assign a value to a variable and pass the variable ( Parameters 1..., Parameters 2......) call '''
f = lambda x,y=0: x+y
print(f(1, 2)) # 3
print(f(2)) # 2
# f = lambda x,y: return x + y Error Unwanted return

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