`
dengyin2000
  • 浏览: 1237061 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

What does ** and * do for python parameters?

阅读更多
The *args and **kwargs ist a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the the python documentation.

The *args will give you all funtion parameters a a list:

In [1]: def foo(*args):
   ...:     for a in args:
   ...:         print a
   ...:         
   ...:         

In [2]: foo(1)
1


In [4]: foo(1,2,3)
1
2
3


The **kwargs will give you all keyword arguments except for those corresponding to a formal parameter as a dictionary.

In [5]: def bar(**kwargs):
   ...:     for a in kwargs:
   ...:         print a, kwargs[a]
   ...:         
   ...:         

In [6]: bar(name="one", age=27)
age 27
name one
分享到:
评论

相关推荐

    google的python编码规范

    """This is a brief description of what the function does.""" # Function body ``` 5. **异常处理:** - 使用`try/except`来捕获和处理异常,但避免过于宽泛的捕获,如`except:`,这可能掩盖问题。 - 提供...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Maintaining a uniform style and following conventions means that we can more easily use "pattern-matching" to infer what various symbols are and what invariants are true about them. Creating common, ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    What does being "passed on the stack" mean exactly? It’s time to take a deeper dive into what happens when a function is called from an assembly standpoint by exploring some “stack related” ...

    MySql存储过程编程.chm

    What This Book Does Not Cover Conventions Used in This Book Which Version? Resources Available at the Book's Web Site Using Code Examples Safari® Enabled How to Contact Us ...

    freemarker总结

    <#if animals.python.price Pythons are cheaper than elephants today. Pythons are not cheaper than elephants today. </#if> 2、 switch , case , default , break指令 这些指令显然是分支...

    大机汇编宏代码展开

    The expandmacro_ply.exe is a command on Windows written by Python(then convert to exe), which can expand the HLASM macro statement and can show the steps it be expanded. What can you do by it? 1....

Global site tag (gtag.js) - Google Analytics