zoukankan      html  css  js  c++  java
  • 参数练习

     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

  • 相关阅读:
    第二次结对编程总结
    结对编程作业博客
    现状、经验和计划
    个人总结
    6月中旬开发心得
    读《软件开发沉思录》
    团队进度汇报
    个人课程总结
    Beta阶段总结
    冲刺第十天 1.11 FRI
  • 原文地址:https://www.cnblogs.com/t-ae/p/10858734.html
Copyright © 2011-2022 走看看