python 确定是否传递了命名参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/255429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if a named parameter was passed
提问by Jake
I would like to know if it is possible to determine if a function parameter with a default value was passed in Python. For example, how does dict.pop work?
我想知道是否可以确定在 Python 中是否传递了具有默认值的函数参数。例如,dict.pop 是如何工作的?
>>> {}.pop('test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop(): dictionary is empty'
>>> {}.pop('test',None)
>>> {}.pop('test',3)
3
>>> {}.pop('test',NotImplemented)
NotImplemented
How does the pop method determine that the first time a default return value was not passed? Is this something that can only be done in C?
pop 方法如何确定第一次没有传递默认返回值?这是只能在 C 中完成的事情吗?
Thanks
谢谢
采纳答案by ddaa
I guess you mean "keyword argument", when you say "named parameter". dict.pop()
does not accept keyword argument, so this part of the question is moot.
当您说“命名参数”时,我猜您的意思是“关键字参数”。dict.pop()
不接受关键字参数,所以这部分问题没有实际意义。
>>> {}.pop('test', d=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pop() takes no keyword arguments
That said, the way to detect whether an argument was provided is to use the *args
or **kwargs
syntax. For example:
也就是说,检测是否提供了参数的方法是使用*args
or**kwargs
语法。例如:
def foo(first, *rest):
if len(rest) > 1:
raise TypeError("foo() expected at most 2 arguments, got %d"
% (len(rest) + 1))
print 'first =', first
if rest:
print 'second =', rest[0]
With some work, and using the **kwargs
syntax too it is possible to completely emulate the python calling convention, where arguments can be either provided by position or by name, and arguments provided multiple times (by position and name) cause an error.
通过一些工作,并使用**kwargs
语法,可以完全模拟 python 调用约定,其中参数可以按位置或按名称提供,并且多次提供参数(按位置和名称)会导致错误。
回答by Markus Jarderot
The convention is often to use arg=None
and use
约定是经常使用arg=None
和使用
def foo(arg=None):
if arg is None:
arg = "default value"
# other stuff
# ...
to check if it was passed or not. Allowing the user to pass None
, which would be interpreted as if the argument was notpassed.
检查它是否通过。允许用户通过None
,这将被解释为好像没有传递参数。
回答by Greg Hewgill
回答by Federico A. Ramponi
def f(one, two=2):
print "I wonder if", two, "has been passed or not..."
f(1, 2)
If this is the exact meaning of your question, I think that there is no way to distinguish between a 2 that was in the default value and a 2 that has been passed. I didn't find how to accomplish such distinction even in the inspectmodule.
如果这是您问题的确切含义,我认为无法区分默认值中的 2 和已传递的 2。即使在检查模块中,我也没有找到如何实现这种区别。
回答by tzot
I am not certain if I fully understand what is it you want; however:
我不确定我是否完全理解你想要什么;然而:
def fun(arg=Ellipsis):
if arg is Ellipsis:
print "No arg provided"
else:
print "arg provided:", repr(arg)
does that do what you want? If not, then as others have suggested, you should declare your function with the *args, **kwargs
syntax and check in the kwargs dict for the parameter existence.
那做你想要的吗?如果没有,那么正如其他人所建议的那样,您应该使用*args, **kwargs
语法声明您的函数,并在 kwargs dict 中检查参数是否存在。