参数练习

 1 def story(**kwds):
 2     return('Once upon a time.there was a %(job)s called %(name)s.'%kwds)
 3 
 4 def power(x,y,*others):
 5     if others:
 6         print('Received redundant parameters:',others)
 7     return pow(x,y)
 8 
 9 def interval(start,stop=None,step=1):
10     'Imitates range() for step>0'
11     if stop is None:
12         start, stop = 0,start
13     result = []
14     i = start
15     while i <stop:
16         result.append(i)
17         i+=step
18     return result

>>> print(story(job='king',name='Gumby'))
Once upon a time.there was a king called Gumby.
>>> print(story(name='Liang',job='leader'))
Once upon a time.there was a leader called Liang.

>>> kwds = {'name':'Morse','job':'detective'}

>>> print(story(**kwds))
Once upon a time.there was a detective called Morse.

>>> del kwds['job']
>>> print(story(job='stroke of genius',**kwds))
Once upon a time.there was a stroke of genius called Morse.

>>> power(2,3)
8
>>> power(3,2)
9
>>> power(y=3,x=2)
8
>>> params = (5,)*2
>>> power(*params)
3125
>>> power(3,3,'hello,world')
Received redundant parameters:hello,world
27
>>> interval(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> interval(1,5)
[1, 2, 3, 4]
>>> power(*interval(3,7)) ###power([3,4,5,6])

Received redundant parameters: (5, 6)
81

posted @ 2019-05-13 20:13  没尾巴的熊  阅读(182)  评论(0编辑  收藏  举报