python自定义函数

运行系统:win7企业版 64位,Python 3.6.5

第一部分

>>> def print_params(**par):

...     print (par)
...
>>> print_params(x=1,y=2,z=3,abc='ttt')
{'x': 1, 'y': 2, 'z': 3, 'abc': 'ttt'}
>>> def printp(x,y,z=3,*pospar,**par):
...     print(x,y,z)
...     print(pospar)
...     print(par)

...

>>> printp(1,2,4,5,6,7,'8af','ewr','f',foo=1,bar=2,abc='sdf')
1 2 4
(5, 6, 7, '8af', 'ewr', 'f')

{'foo': 1, 'bar': 2, 'abc': 'sdf'}

第二部分

>>> def add(x,y):return x+y
...
>>> params=(1,2)
>>> add(params)
Traceback (most recent call last):
  File "", line 1, in
TypeError: add() missing 1 required positional argument: 'y'
>>> add(*params)

3

字典同理

第三部分

def story(**kwds):
return 'Once upon a time,there was a %(job)s called %(name)s.' % kwds


def power(x,y,*others):
if others:
print('Received redunandt parameters;',others)
return pow(x,y)


def interval(start,stop=None,step=1):
'Imitates range() for step>0'
if stop is None:
start,stop=0,start
result=[]
i = start
while i result.append(i);
i+=step
return result

你可能感兴趣的:(python)