如何获取 class 方法中定义的所有变量的名称
How to get names of all the variables defined in methods of a class
我有以下 class:
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = something
def bar(self):
var3 = some value
def baz(self, var):
var4 = some value
我想打印方法中定义的所有变量的名称,例如:
setUp, bar, baz, var1, var2, var3, var4
我试过使用 locals(), vars(), globals()
但我只得到方法的名称而不是变量名称。
我也尝试使用 ast
模块,但没有成功。
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = "something"
def bar(self):
var3 = "some value"
def baz(self, var):
var5 = 34
print Foo.setUp.__code__.co_varnames
print Foo.bar.__code__.co_varnames
print Foo.baz.__code__.co_varnames
Output:
('self',)
('self', 'var3')
('self', 'var', 'var5')
#There are many more things in __code__ dict:
# '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
还不能发表评论(声誉不够),但这看起来像是 Iterate over object attributes in python 的副本。
试试这个:
print([a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj,a))])
您可以使用 ast.parse
to generate AST node. Then ast.walk
可用于递归迭代节点及其后代。对于每个节点,您可以检查类型,然后提取正确的属性。
下面是一个使用您的代码的示例,但不要指望它能像这样处理更复杂的文件:
source = '''
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = 1
def bar(self):
var3 = 2
def baz(self, var):
var4 = var
'''
import ast
def hack(source):
root = ast.parse(source)
for node in ast.walk(root):
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
yield node.id
elif isinstance(node, ast.Attribute):
yield node.attr
elif isinstance(node, ast.FunctionDef):
yield node.name
print(list(hack(source)))
输出:
['setUp', 'bar', 'baz', 'var1', 'var2', 'var3', 'var4']
我有以下 class:
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = something
def bar(self):
var3 = some value
def baz(self, var):
var4 = some value
我想打印方法中定义的所有变量的名称,例如:
setUp, bar, baz, var1, var2, var3, var4
我试过使用 locals(), vars(), globals()
但我只得到方法的名称而不是变量名称。
我也尝试使用 ast
模块,但没有成功。
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = "something"
def bar(self):
var3 = "some value"
def baz(self, var):
var5 = 34
print Foo.setUp.__code__.co_varnames
print Foo.bar.__code__.co_varnames
print Foo.baz.__code__.co_varnames
Output:
('self',)
('self', 'var3')
('self', 'var', 'var5')
#There are many more things in __code__ dict:
# '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames']
还不能发表评论(声誉不够),但这看起来像是 Iterate over object attributes in python 的副本。
试试这个:
print([a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj,a))])
您可以使用 ast.parse
to generate AST node. Then ast.walk
可用于递归迭代节点及其后代。对于每个节点,您可以检查类型,然后提取正确的属性。
下面是一个使用您的代码的示例,但不要指望它能像这样处理更复杂的文件:
source = '''
class Foo(object):
def setUp(self):
self.var1 = "some value"
self.var2 = 1
def bar(self):
var3 = 2
def baz(self, var):
var4 = var
'''
import ast
def hack(source):
root = ast.parse(source)
for node in ast.walk(root):
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
yield node.id
elif isinstance(node, ast.Attribute):
yield node.attr
elif isinstance(node, ast.FunctionDef):
yield node.name
print(list(hack(source)))
输出:
['setUp', 'bar', 'baz', 'var1', 'var2', 'var3', 'var4']