如何获取Python中所有已初始化对象和函数定义的列表?

发布于 2024-10-08 08:50:28 字数 108 浏览 0 评论 0原文

假设在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小红帽 2024-10-15 08:50:28

是的。

>>> import gc
>>> gc.get_objects()

并不是说你会发现它很有用。其中有很多。 :-) 当你启动 Python 时就超过 4000。

可能更有用的是所有本地活跃的变量:

>>> locals()

以及全局活跃的变量:(

>>> globals()

请注意,Python 中的“全局”并不是真正的全局。为此,您需要 上面的 gc.get_objects() ,并且您不太可能发现有用,如前所述)。

Yes.

>>> import gc
>>> gc.get_objects()

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:

>>> locals()

And the one active globally:

>>> globals()

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

可遇━不可求 2024-10-15 08:50:28

函数gc.get_objects()不会找到所有对象,例如不会找到numpy数组。

import numpy as np
import gc

a = np.random.rand(100)
objects = gc.get_objects()
print(any[x is a for x in objects])
# will not find the numpy array

您将需要一个扩展所有对象的函数,如此处

# code from https://utcc.utoronto.ca/~cks/space/blog/python/GetAllObjects
import gc
# Recursively expand slist's objects
# into olist, using seen to track
# already processed objects.
def _getr(slist, olist, seen):
  for e in slist:
    if id(e) in seen:
      continue
    seen[id(e)] = None
    olist.append(e)
    tl = gc.get_referents(e)
    if tl:
      _getr(tl, olist, seen)

# The public function.
def get_all_objects():
  """Return a list of all live Python
  objects, not including the list itself."""
  gcl = gc.get_objects()
  olist = []
  seen = {}
  # Just in case:
  seen[id(gcl)] = None
  seen[id(olist)] = None
  seen[id(seen)] = None
  # _getr does the real work.
  _getr(gcl, olist, seen)
  return olist

所述我们应该能够找到大多数个对象

import numpy as np
import gc

a = np.random.rand(100)
objects = get_all_objects()
print(any[x is a for x in objects])
# will return True, the np.ndarray is found!

The function gc.get_objects() will not find all objects, e.g. numpy arrays will not be found.

import numpy as np
import gc

a = np.random.rand(100)
objects = gc.get_objects()
print(any[x is a for x in objects])
# will not find the numpy array

You will need a function that expands all objects, as explained here

# code from https://utcc.utoronto.ca/~cks/space/blog/python/GetAllObjects
import gc
# Recursively expand slist's objects
# into olist, using seen to track
# already processed objects.
def _getr(slist, olist, seen):
  for e in slist:
    if id(e) in seen:
      continue
    seen[id(e)] = None
    olist.append(e)
    tl = gc.get_referents(e)
    if tl:
      _getr(tl, olist, seen)

# The public function.
def get_all_objects():
  """Return a list of all live Python
  objects, not including the list itself."""
  gcl = gc.get_objects()
  olist = []
  seen = {}
  # Just in case:
  seen[id(gcl)] = None
  seen[id(olist)] = None
  seen[id(seen)] = None
  # _getr does the real work.
  _getr(gcl, olist, seen)
  return olist

Now we should be able to find most objects

import numpy as np
import gc

a = np.random.rand(100)
objects = get_all_objects()
print(any[x is a for x in objects])
# will return True, the np.ndarray is found!
等待圉鍢 2024-10-15 08:50:28

将实例化对象输出为列表的 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')]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文