python函数获取 *arg名称
给定一个python函数,
def func(a, b, *c, **d):
pass
我可以得到参数的名称:
>>> print(func.__code__.co_varnames)
('a', 'b', 'c', 'd')
但是我怎么知道,c是 *args,d是** kwargs?
Given a python function
def func(a, b, *c, **d):
pass
I can get the names of arguments:
>>> print(func.__code__.co_varnames)
('a', 'b', 'c', 'd')
But how do I know, that c is *args and d is **kwargs??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,仅通过
签名
对象访问Inspect.Signature.signature
才能访问此信息。其中 函数获取信息,我不知道。 (这可能是实施中存储的东西,以其他方式在Python级别上没有暴露。)如果您查看
_signature_from_function
,您会看到它通过查看func .__ Code __. co_objects 并对您在
func .__代码__. co_varnames
中看到的名称上的左右逻辑应用一些逻辑。As far as I know, this information is only accessible via the
Signature
object returned byinspect.signature
. Where that function gets the information, I do not know. (It may be something stored by the implementation that is not otherwise exposed at the Python level.)If you look at the definition of
_signature_from_function
, you'll see it determines the kind by looking at the flags infunc.__code__.co_objects
and applying some left-to-right logic on the names you see infunc.__code__.co_varnames
.