How to get names of all the variables defined in methods of a class

↘锁芯ラ 提交于 2021-02-19 02:36:06

问题


I have the following 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

I want to print the names of all variables defined inside the methods, like:

setUp, bar, baz, var1, var2, var3, var4

I have tried using locals(), vars(), globals() but I am getting only the names of method and not variable names.

I also tried using ast module, but no success.


回答1:


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']



回答2:


You can use ast.parse to generate AST node. Then ast.walk can be used to recursively iterate over the node and its` descendants. For each node you can check the type and then extract the correct attribute.

Below is an example that is working with your code but don't expect it to work as such with more complex files:

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)))

Output:

['setUp', 'bar', 'baz', 'var1', 'var2', 'var3', 'var4']



回答3:


Can't comment yet (not enough reputation), but this looks like a duplicate of Iterate over object attributes in python.

Try this:

print([a for a in dir(obj) if not a.startswith('__') and not callable(getattr(obj,a))])


来源:https://stackoverflow.com/questions/41115160/how-to-get-names-of-all-the-variables-defined-in-methods-of-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!