简体   繁体   English

在python中将默认的第一个参数而不是第二个设置为什么?

[英]What to put to default first argument and not second in python?

Let's say I have a function 假设我有一个功能

def function(name="Default", age=16):
    print("My name is", name, "and I am", age)

How do I call the function to default the first parameter, but not the second? 如何调用该函数以默认第一个参数,而不是第二个? In the above function, I would like to have name be its default value ( "Default" ), but input the age as 200 . 在上面的函数中,我想将name作为其默认值( "Default" ),但将age输入为200

I have already tried function(,200) ; 我已经尝试过function(,200) ; however, it says SyntaxError: invalid syntax , pointing at the , in the method call. 然而,它说SyntaxError: invalid syntax ,在指点,在方法调用。

在不使用name参数的情况下调用上述函数的正确方法是,指定要更改其默认值的参数,如下所示。

function(age=200)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python C API中,如果没有设置第二个参数怎么能默认为第一个的值? - In the Python C API, if the second argument is not set how can I default to the value of the first? 在Python中,'覆盖'默认参数为0的原因是什么? - In Python, what is the reason for 'overwriting' a default argument of 0? Python getopt将第二个选项作为第一个选项的参数 - Python getopt taking second option as argument of first option 如何从第二个引用函数的第一个参数? (python 2.7) - How to reference first argument of function from second? (python 2.7) Python 中用于从字符串转换为 int 的代码中的第二个参数是什么? - What is the second argument in the code used to convert from string to int in Python? 在Python类参数中加点是什么意思? - What does it mean to put a dot in a Python class argument? Python:如何获取str列表中的每个元素,然后将第一个元素放入目录中的第一个文件中,第二个元素放入第二个目录中? - Python: how to get each element in a str list and then put the first element into the first file in a directory, second element in the second dir? 避免默认参数作为无列表的python 方法是什么? - What is the python way to avoid a default argument as a list of Nones? 如何在Python中默认为默认参数 - How to default to the default argument in Python Python 的第一个参数是什么? sys.argv[0] 或 sys.argv[1] - What is Python first argument? sys.argv[0] or sys.argv[1]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM