zoukankan      html  css  js  c++  java
  • *args 和 **kwargs

    def test_var_args(f_arg, *argv):
        print("first normal arg:", f_arg)
        for arg in argv:
            print("another arg through *argv:", arg)
    
    test_var_args('yasoob', 'python', 'eggs', 'test')
    
    first normal arg: yasoob
    another arg through *argv: python
    another arg through *argv: eggs
    another arg through *argv: test
    
    def greet_me(**kwargs):
        for key, value in kwargs.items():
            print("{0} = {1}".format(key, value))
    
    >>> greet_me(name="yasoob")
    name = yasoob
    
    def test_args_kwargs(arg1, arg2, arg3):
        print("arg1:", arg1)
        print("arg2:", arg2)
        print("arg3:", arg3)
    
    # first with *args
    >>> args = ("two", 3, 5)
    >>> test_args_kwargs(*args)
    arg1: two
    arg2: 3
    arg3: 5
    
    # now with **kwargs:
    >>> kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
    >>> test_args_kwargs(**kwargs)
    arg1: 5
    arg2: two
    arg3: 3
    
  • 相关阅读:
    团队冲刺03
    梦断代码 阅读笔记02
    团队冲刺02
    团队冲刺01
    周总结
    团队工作任务
    阅读笔记3
    梦断代码阅读笔记01
    周总结
    NABCD项目分析
  • 原文地址:https://www.cnblogs.com/yaos/p/14014332.html
Copyright © 2011-2022 走看看