Python Introspection: How to get varnames of class methods?

混江龙づ霸主 提交于 2019-12-05 10:46:26

Well, as a direct extension of what you did:

for varname in a.__class__.__dict__['A1'].__code__.co_varnames:
    print varname

prints:

self
test1

P.S.: to be honest, I have a feeling this can be done more elegantly...

For example, you can replace a.__class__ with A, but you knew that ;-)

import inspect

for name, method in inspect.getmembers(a, inspect.ismethod):
    print name
    (args, varargs, varkw, defaults) = inspect.getargspec(method)
    for arg in args:
        print arg
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!