我想知道在cpython中是否可以进行以下内省:
>>> def potato(x=69):
... if x == 69 and ???:
... print '69 was taken from argument defaults'
... if x == 69 and ???:
... print '69 was passed as positional arg'
... if x == 69 and ???:
... print '69 was passed as kwarg'
...
>>> potato()
69 was taken from argument defaults
>>> potato(69)
69 was passed as positional arg
>>> potato(x=69)
69 was passed as kwarg
我对python2和python3的答案感兴趣,如果它们不同的话。
任何涉及inspect
,traceback
,pdb
,sys._getframe
等的blackmagic都是允许的。当然,不允许修改函数的argspec。
答案 0 :(得分:1)
虽然框架有一个名为 code_context 的字符串,但它看起来像 inspect 可以直接提供此信息,它会为您提供调用该函数的源代码行。问题是人们必须重写一个小的解析器来理解它。
这是一个更简单的解决方案,它基于您要检查的函数的包装器。它不会更改arg规范,也不会更改arg验证:
import inspect
def track_args(func):
def tracker(*args, **kwargs):
r = func(*args, **kwargs)
for arg_num,arg_name in enumerate(inspect.getargspec(func)[0]):
if arg_name in kwargs:
print "%s was provided as keyword arg" % arg_name
elif arg_num < len(args):
print "%s was provided as positional arg" % arg_name
else:
print "%s was provided by default value" % arg_name
return r
return tracker
@track_args
def f(a, b, c=30):
print "I'm f()"
f(20, b=10)
f(20)
有效参数的结果:
I'm f()
a was provided as positional arg
b was provided as keyword arg
c was provided by default value
参数无效的结果:
Traceback (most recent call last):
File "test.py", line 21, in <module>
f(20)
File "test.py", line 5, in tracker
r = func(*args, **kwargs)
TypeError: f() takes at least 2 arguments (1 given)