简体   繁体   English

Python如何列出已导入到类中的所有方法/类

[英]Python How to list all methods/classes that have been imported into a class

How to list all methods/classes that have been imported into a class. 如何列出已导入到类的所有方法/类。 Take an example 举个例子

from module1 import method1
from module2 import method2

class foo(object):
   def say_foo(self):
      method1()
   def talk(self):
      method2()

is it possible to list all methods used in the foo class? 是否可以列出foo类中使用的所有方法? i know inspect.getmembers(foo) will list talk and say_foo , how do i list method1 and method2 ? 我知道inspect.getmembers(foo)将列出talksay_foo ,如何列出method1method2

To be brutally honest, your question doesn't make a whole lot of sense, and here is why: 坦率地说,您的问题没有任何意义,这是为什么:

At the point when foo is defined, no connection is made between the two globals called by say_foo and talk and the two functions imported at the top of your code. 在定义foo那一刻,在say_footalk调用的两个全局变量与在代码顶部导入的两个函数之间没有建立连接。

For example, the interpreter will not complain about the following: 例如,解释器不会抱怨以下内容:

class foo(object):
   def say_foo(self):
      method1()
   def talk(self):
      method2()

Despite the absence of imports, this is still valid code. 尽管没有导入,但这仍然是有效的代码。 You won't of course be able to call say_foo and talk without getting a NameError . 在没有得到NameError情况下,您当然不能调用say_foo并进行talk

The following is also fine: 以下内容也可以:

from module1 import method1
from module2 import method2

class foo(object):
   def say_foo(self):
      method1()
   def talk(self):
      method2()

obj1 = foo()
obj1.say_foo()

def method1(): pass

obj1.say_foo()
obj2 = foo()
obj2.say_foo()

I leave to you to figure out which method1 gets invoked when. 我留给您找出哪个method1何时被调用。

Now, having said all that, there is something you can do. 综上所述,现在您可以做些事情。 If you examine the method object's co_names , that'll give you the list of names that are used by the method : 如果检查方法对象的co_names ,将为您提供该方法使用的名称列表

In [30]: foo.say_foo.im_func.func_code.co_names
Out[30]: ('method1',)

In [31]: foo.talk.im_func.func_code.co_names
Out[31]: ('method2',)

This clearly isn't exactly the same as what you're asking , and may or may not be useful depending on what you intend to do with this. 这显然与您要的内容不完全相同 ,并且可能有用也可能没有用,这取决于您打算如何处理。

It is also almost certainly CPython-specific. 也几乎可以肯定它是特定于CPython的。

use ast module to analyze you code: 使用ast模块来分析您的代码:

code = """
from module1 import method1
from module2 import method2

class foo(object):
   def say_foo(self):
      method1()
   def talk(self):
      method2()
"""

import ast, _ast

t = ast.parse(code)
for node in ast.walk(t):
    if isinstance(node, ast.ClassDef) and node.name == "foo":
        klass = node
        break

for node in ast.walk(klass):
    if isinstance(node, _ast.Call):
        print node.func.id

the output is : 输出为:

method1
method2

What? 什么? method1 and method2 haven't been "imported into" the foo class. method1method2尚未“导入” foo类。 They're imported in the module, so dir(foomodule) will show them. 它们被导入到模块中,因此dir(foomodule)将显示它们。 But there's simply no reference from foo to method1 . 但是从foomethod1根本没有引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM