【发布时间】:2013-01-03 15:28:30
【问题描述】:
关注这个话题:allow users to "extend" API functions
class Inspector:
def __init__(self, passedFunc):
self.passedFunc = passedFunc
def __call__(self, *args, **kwargs):
# Inspector needs to generate a list of arguments of the passed function and print them in *correct* order
# now how to generate a list of arguments using data from both ''args' and 'kwargs'?
# the user might keep default argument values, in which case both will be None,
# he might only use args, in which case it is easy,
# but he might also use kwargs, in which case I don't see how to generate a list with correct order of arguments
# and he might use all 3 from above together
# I found this solution for when only default arguments are used
if args == () and kwargs == {}: args = self.passedFunc.__defaults__
print args
@Inspector
def exampleFunc(value=0, otherValue=3):
pass
exampleFunc()
如何为所有场景生成正确的order args列表?
【问题讨论】:
-
由于
kwargs是dict,没有顺序,我认为不可能。 -
kwargs 是“关键字参数”,它是一个字典,字典没有在 python 中排序,因此它们没有“顺序”,但您始终可以按您想要的任何方式按其键或值对字典进行排序- 但他们没有预设定义的顺序。
-
我知道,这就是我问的原因,也许还有其他方法。也许使用类似于
__defaults__的东西获取订单并从字典中获取它们的值
标签: python arguments decorator args keyword-argument