zoukankan      html  css  js  c++  java
  • python,keyword arguments

    http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html


    4.7.2 Keyword Arguments

    Functions can also be called using keyword arguments of the form "keyword = value". For instance, the following function:

    def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
        print "-- This parrot wouldn't", action,
        print "if you put", voltage, "Volts through it."
        print "-- Lovely plumage, the", type
        print "-- It's", state, "!"
    

    could be called in any of the following ways:

    parrot(1000)
    parrot(action = 'VOOOOOM', voltage = 1000000)
    parrot('a thousand', state = 'pushing up the daisies')
    parrot('a million', 'bereft of life', 'jump')
    

    but the following calls would all be invalid:

    parrot()                     # required argument missing
    parrot(voltage=5.0, 'dead')  # non-keyword argument following keyword
    parrot(110, voltage=220)     # duplicate value for argument
    parrot(actor='John Cleese')  # unknown keyword
    

    In general, an argument list must have any positional arguments followed by any keyword arguments, where the keywords must be chosen from the formal parameter names. It's not important whether a formal parameter has a default value or not. No argument must receive a value more than once -- formal parameter names corresponding to positional arguments cannot be used as keywords in the same calls.

    When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments whose keyword doesn't correspond to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.) For example, if we define a function like this:

    def cheeseshop(kind, *arguments, **keywords):
        print "-- Do you have any", kind, '?'
        print "-- I'm sorry, we're all out of", kind
        for arg in arguments: print arg
        print '-'*40
        for kw in keywords.keys(): print kw, ':', keywords[kw]
    

    It could be called like this:

    cheeseshop('Limburger', "It's very runny, sir.",
               "It's really very, VERY runny, sir.",
               client='John Cleese',
               shopkeeper='Michael Palin',
               sketch='Cheese Shop Sketch')
    

    and of course it would print:

    -- Do you have any Limburger ?
    -- I'm sorry, we're all out of Limburger
    It's very runny, sir.
    It's really very, VERY runny, sir.
    ----------------------------------------
    client : John Cleese
    shopkeeper : Michael Palin
    sketch : Cheese Shop Sketch
    
  • 相关阅读:
    Intersection Observer 观察元素何时进入或退出浏览器的视口
    JS防抖函数、节流函数工具类封装
    页面刷新时,如何保持原有vuex中的state信息
    vue具名插槽、作用域插槽的新写法
    vue-cli3+工具中,配置路径别名(vue.config.js)
    基于Vue-SSR优化方案归纳总结
    React组件设计:重新认识受控与非受控组件
    如何用 JavaScript 实现一个数组惰性求值库
    原生 js 中应该禁止出现的写法,以提高代码效率和安全性
    Javascript 实践中的命令模式
  • 原文地址:https://www.cnblogs.com/threef/p/3209305.html
Copyright © 2011-2022 走看看