How to use *args and **kwargs in Python
2011-09-20 18:48
676 查看
This also means how to use variable length argument lists in Python.
The special syntax,*argsand
**kwargsin function definitions is used to pass a variable number of arguments to a function. The single asterisk form (
*args) is used to pass anon-keyworded, variable-length
argument list, and the double asterisk form is used to pass akeyworded, variable-length argument list. Here is an example of how to use the non-keyworded form. This example passes one formal (positional) argument, and two more variable length arguments.
def test_var_args(farg, *args): print "formal arg:", farg for arg in args: print "another arg:", arg test_var_args(1, "two", 3)
Results:
formal arg: 1 another arg: two another arg: 3
Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.
def test_var_kwargs(farg, **kwargs): print "formal arg:", farg for key in kwargs: print "another keyword arg: %s: %s" % (key, kwargs[key]) test_var_kwargs(farg=1, myarg2="two", myarg3=3)
Results:
formal arg: 1 another keyword arg: myarg2: two another keyword arg: myarg3: 3
Using *args
and **kwargs
when calling a function
This special syntax can be used, not only in function definitions, but also whencalling a function.def test_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3 args = (1, "two", 3) test_var_args_call(*args)
Results:
arg1: 1 arg2: two arg3: 3
Here is an example using the keyworded form when calling a function:
def test_var_args_call(arg1, arg2, arg3): print "arg1:", arg1 print "arg2:", arg2 print "arg3:", arg3 kwargs = {"arg3": 3, "arg2": "two", "arg1": 1} test_var_args_call(**kwargs)
Results:
arg1: 1 arg2: two arg3: 3
See also (updated 2009-04-12):
Section 4.7More on Defining Functions in the
Official Python Tutorial
PEP 3102 Keyword-Only Arguments
Section 5.3.4Calls in the
Python Reference Manual
相关文章推荐
- [zz]How to use *args and **kwargs in Python
- 如何在python中使用*args,**kwargs (How to use *args and **kwargs in Python)
- How to use *args and **kwargs in Python
- QT29 how to use QWebView and open web page in QWebView
- How to Manage and Use LVM (Logical Volume Management) in Ubuntu In our previous article we told you
- How to use UTF-8 in Python
- How to use git in general and bitbucket in particular
- How to use HttpWebRequest and HttpWebResponse in .NET
- How to use $http and $resource in Angular JS
- QT30 how to use QcheckBox and QRadioButton in qt
- How to use the System Restore API to save and to restore system data in Visual C++
- c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- ACC97: How to Use Percent, Plus, and Caret Symbols in ASP Forms
- How to use a 32bit Oracle11_g client in 64 win system and not conflict with sqldeveloper 64 bit tool
- How to Use Auto Layout in XCode 6 for iOS 7 and 8 Development
- how to make Completion Handlers and use it in Swift
- How to use composition and inheritance in visual c# ?
- How to use QToolBar and QToolButton in Qt
- How to use Comparator and Comparable in Java? With example