自定义函数(def):形参和实参

1.函数文档

>>> def say(name, words):
	'函数定义阶段, name, words叫形参'
	'传递进来 name words是实参'
	print(name + ' say: ' + words)

	
>>> say('itxds', 'hello')
itxds say: hello
>>> 

 

2.打印文档

	
>>> say('itxds', 'hello')
itxds say: hello
>>> say.__doc__
'函数定义阶段, name, words叫形参'
>>> help(say)
Help on function say in module __main__:

say(name, words)
    函数定义阶段, name, words叫形参

>>> 

 

3.关键字参数

  使用顺序参数时,如果函数的参数多达十几个甚至几十个,这样在调用的时候很容出错。

>>> def say(name, words):
	'函数定义阶段, name, words叫形参'
	'传递进来 name words是实参'
	print(name + ' say: ' + words)

	
>>> say(words='hello', name='itxds')
itxds say: hello
>>> 

 

4.默认参数

>>> def say(name = 'itxds', words = 'hello'):
	'函数定义阶段, name, words叫形参'
	'传递进来 name words是实参'
	print(name + ' say: ' + words)

	
>>> say()
itxds say: hello
>>> 

5.收集参数

  在调用函数时,系统将收集参数以元组的形式传递给函数内部调用

>>> def say(*params):
	print(params)

	
>>> say(1,3,4,5,6,7,8)
(1, 3, 4, 5, 6, 7, 8)
>>> 

6.收集参数与关键字参数结合使用

>>> def say(*params, name='itxds'):
	print(name)
	print(params)

	
>>> 
>>> say(1,2,4,5, name='zhangsna')
zhangsna
(1, 2, 4, 5)
>>> 

  

  

posted @ 2020-08-01 22:57  coder_xds  阅读(705)  评论(0编辑  收藏  举报