如何获取Python中所有已初始化对象和函数定义的列表?
假设在 python shell (IDLE) 中我定义了一些类、函数、变量。还创建了类的对象。然后我删除了一些对象并创建了其他一些对象。稍后,我如何才能知道内存中当前活动的对象、变量和方法定义是什么?
Say that in the python shell (IDLE) I have defined some classes, functions, variables. Also created objects of the classes. Then I deleted some of the objects and created some others. At a later point in time, how can I get to know what are the currently active objects, variables, and methods definitions active in the memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。
并不是说你会发现它很有用。其中有很多。 :-) 当你启动 Python 时就超过 4000。
可能更有用的是所有本地活跃的变量:
以及全局活跃的变量:(
请注意,Python 中的“全局”并不是真正的全局。为此,您需要
上面的 gc.get_objects()
,并且您不太可能发现有用,如前所述)。Yes.
Not that you'll find that useful. There is a lot of them. :-) Over 4000 just when you start Python.
Possibly a bit more useful is all the variables active locally:
And the one active globally:
(Note that "globally" in Python isn't really global as such. For that, you need the
gc.get_objects()
above, and that you are unlikely to ever find useful, as mentioned).函数
gc.get_objects()
不会找到所有对象,例如不会找到numpy数组。您将需要一个扩展所有对象的函数,如此处
所述我们应该能够找到大多数个对象
The function
gc.get_objects()
will not find all objects, e.g. numpy arrays will not be found.You will need a function that expands all objects, as explained here
Now we should be able to find most objects
将实例化对象输出为列表的
dir()
怎么样?我刚刚用过这个:[x for x in dir() if x.lower().startswith('y')]
How about
dir()
which will output instantiated objects as a list? I just used this just now:[x for x in dir() if x.lower().startswith('y')]
尝试
globals()
Try
globals()