Flexibility 4.2 Functions
Function parameters
1. formal and actual parameters
1) in the form of parameters (parameter)
Usually the function value of the parameter definition process small brackets
def func1(name):
'函数定义过程中的name叫形参'
#因为它只是一个形式,表示占据一个参数位置
print('参数进来的' + name + '叫做实参,因为它是具体的参数值!')
func1("weivid")
Parameters are passed in weivid, argument is, because it is specific parameters
2) the actual parameter (argument)
Function parameters passed in the procedure call
2. Function Documentation
The above function 'function definition process is called parameter name' documents belonging function
Similar features and annotations
def func1(name):
'函数定义过程中的name叫形参'
#因为它只是一个形式,表示占据一个参数位置
print('参数进来的' + name + '叫做实参,因为它是具体的参数值!')
func1("weivid") #调用
print(func1.__doc__)
See default property function, generally by double underline name plus plus the double underline indicates the default properties of the function, i.e., the subsequent objects
may be used to view help
(help(func1))
3. keyword arguments
Keyword arguments given, it will not default order of the index, the index will use the keyword
when the function call to the keyword index
print('关键字参数')
def saysome(name,word):
print(name + '->' + word)
saysome('I love','weivid')
saysome('weivid','I love')
To define the parameters for a keyword
saysome(word = 'weivid',name = 'I love')
4. The default parameters define the default parameter values
print('默认参数')
def saysome1(name='I love',word = 'weivid'):
print(name + '->' + word)
#使用实参调用也可以打印,因为函数有默认值
saysome1()
saysome1('nihao') # 默认将nihao传递给了name参数
The default parameters and keyword parameters difference:
The default parameter is a function defined at the time had been given initial form in the initial parameter assignment without the argument, the default parameters to pass
keyword arguments when the function is called , arguments preceded keyword index assignment
The collected parameters (variable parameter)
Generally only occur in python, other programming languages rarely this parameter
1) When the function does not know how many parameters, you can use this function to collect
added before parameters **
def test(*params): #也可以使用**params
print('the length of the param is:',len(params))
print('第二个参数是:',params[1])
test(1, 'weivid',2.14,3,45) #有五个参数,长度为5, 第二个参数时weivid
2) when using collection and other parameters coexistence of other parameters must use the keyword index
def test(*params,exp): #也可以使用**params
print('the length of the param is:',len(params),exp)
print('第二个参数是:',params[1])
test(1, 'weivid',2.14,3,45,exp = 8) #有五个参数,长度为5, 第二个参数时weivid
Or when the defined function exp = 8, a default parameter set
3) The following is an example of a dictionary
def profile(first,last,**user_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key,value in user_info.items():
profile[key] = value
return profile #这个函数的返回值是一个profile字典,后续说到
user_profile = profile('albert', 'einstein', lacation = 'princeton',field = 'physics')
print(user_profile)
4) print function is actually a collection of parameters
print('查看help帮助文档')
print(help(print))
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)