[英]how to give value only to second parameter of a function with default values in python
def func(a=2,b=3):
print(a*b)
so this code prints 6 if you call:因此,如果您调用,此代码将打印 6:
func()
and prints 10 if you call如果你打电话,打印 10
func(5,2)
so how do you give the second parameter a value and leave the first one with the default?那么如何给第二个参数一个值,而将第一个参数保留为默认值呢? I already tried
我已经试过了
func(,4)
but doesn't work this looks good:但不起作用这看起来不错:
funk(b=4)
but i want to read the values that might or might not be there from an xml file like this:但我想从这样的 xml 文件中读取可能存在或不存在的值:
{"a,b":"2,4"}
or this:或这个:
{"a,b":",4"}
what do you suggest?你有什么建议?
Just do func(b=4)
.只需执行
func(b=4)
。 This is the beauty of keyword arguments, one of my favourite features of Python :)这就是关键字参数的美妙之处,也是我最喜欢的 Python 特性之一 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.