普通参数与关键字参数“关键字参数”与常规参数有什么不同?不能把所有的参数都作为name=value而不是使用位置语法?
3 回答

胡说叔叔
TA贡献1804条经验 获得超8个赞
def my_function(arg1, arg2, **kwargs)
def my_function(**kwargs): print str(kwargs)my_function(a=12, b="abc"){'a': 12, 'b': 'abc'}

汪汪一只猫
TA贡献1898条经验 获得超8个赞
def foo(*positional, **keywords): print "Positional:", positional print "Keywords:", keywords
*positional
foo()
>>> foo('one', 'two', 'three')Positional: ('one', 'two', 'three')Keywords: {}
**keywords
>>> foo(a='one', b='two', c='three')Positional: ()Keywords: {'a': 'one', 'c': 'three', 'b': 'two'}
>>> foo('one','two',c='three',d='four')Positional: ('one', 'two')Keywords: {'c': 'three', 'd': 'four'}

当年话下
TA贡献1890条经验 获得超9个赞
def foo(bar, baz): passfoo(1, 2)foo(baz=2, bar=1)
添加回答
举报
0/150
提交
取消