简体   繁体   English

如何在python中仅给具有默认值的函数的第二个参数赋值

[英]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.

相关问题 如何知道function参数值是否是python中的默认值 - How to know if a function parameter value is the default in python 给可选参数一个默认值,该默认值取决于Python中的另一个参数 - Give an optional parameter a default value that depends on another parameter in Python Python:参数类型为function时如何设置默认值? - Python: How to set the default value when the parameter type is function? 如何在python中仅调用类的第二个值,同时将第一个值保持为默认值? - How to call only the second value of a class in python, while keeping the first value as default? Python函数为空白条目提供默认值不起作用 - Python function to give default value for blank entry not working Python 给 *args 默认值 - Python give *args default value 如何在 python 中制作一个 function,它仅获取数字值的正方形,并且在由字符串调用时不给出错误消息 - How to make a function in python that gets the square for only digits values and does not give an error message when invoked by a string 如何给出要在Python中打印的函数的返回值 - How to give return value of a function to print in Python 如何更改可选函数参数的默认值 - How to change default value of optional function parameter 如何在python中获取函数的默认参数? - How to get a function's default parameter in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM