|      1ekeyme      2017-08-23 17:14:14 +08:00 print(b.__module__) print(a.__module__) | 
|  |      2twistoy      2017-08-23 17:19:31 +08:00 如果是 from foo import bar a = bar() 那这个 a 算是引入的还是不引入的 | 
|  |      3hl      2017-08-23 18:03:08 +08:00 https://docs.python.org/2/library/inspect.html#types-and-members __module__ name of module in which this class was defined In [1]: class a: ...: pass ...: In [2]: b = a() In [3]: from hashlib import md5 In [4]: c = md5() In [24]: inspect.getmodule(a) Out[24]: <module '__main__'> In [25]: inspect.getmodule(b) Out[25]: <module '__main__'> In [27]: inspect.getmodule(c) In [28]: inspect.getmodule(md5) Out[28]: <module '_hashlib' from '/Users/liuqian/anaconda/lib/python3.6/lib-dynload/_hashlib.cpython-36m-darwin.so'> | 
|  |      4jmc891205      2017-08-24 10:29:42 +08:00 虽然你不让问 但我还是想问 为什么有这种需求? | 
|      5modm OP @jmc891205 想扫描一个 package 下面某个 Class 的所有 instance,并记录其文件路径, 如果有引用的情况,扫描的时候这个 instance 会有重复,无法知道真正的路径是哪。 | 
|      7modm OP 打算用  inspect.getsource(module) ,先通过代码正则判断是不是引用的了 |