如何查看所有当前对象及其立即方法

2024-06-08 16:56:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在看TwistedPython中的这个教程。 https://github.com/jdavisp3/twisted-intro/blob/master/twisted-client-3/get-poetry.py

def get_poetry(host, port, callback):
    """
    Download a poem from the given host and port and invoke

      callback(poem)

    when the poem is complete.
    """
    from twisted.internet import reactor
    factory = PoetryClientFactory(callback)#I am interested in checking the instances alive here
    reactor.connectTCP(host, port, factory)


def poetry_main():
addresses = parse_args()

from twisted.internet import reactor

poems = []

def got_poem(poem):
    poems.append(poem)
    if len(poems) == len(addresses):
        reactor.stop()

for address in addresses:
    host, port = address
    get_poetry(host, port, got_poem)

reactor.run()

for poem in poems:
    print poem


if __name__ == '__main__':
    poetry_main()

我以前从未真正调试过python。你知道吗

我想看看哪些类的实例在反应堆停止开火。你知道吗

我在查这个Printing all instances of a class

使用此代码

import gc
for obj in gc.get_objects():

如何有选择地查看最上面的信息,然后进一步查看继承的数据等等?你知道吗

从扭曲的角度来看,我想看看哪些工厂实例当前处于活动状态,以及它与协议的关系如何


Tags: theinfromimporthostgetpoetrymain
1条回答
网友
1楼 · 发布于 2024-06-08 16:56:30

但是,如果您真的只是想了解如何调试Python,请查看“dir(obj)”,它将列出对象的所有属性和方法。你知道吗

class Blah(object):
    pass

b = Blah()

for x in dir(b):
    try:
        print getattr(b,x,False)
    except Exception, e:
        print x,e

将产生:

<class '__main__.Blah'>
<method-wrapper '__delattr__' of Blah object at 0x1028ba490>
{}
None
<built-in method __format__ of Blah object at 0x1028ba490>
<method-wrapper '__getattribute__' of Blah object at 0x1028ba490>
<method-wrapper '__hash__' of Blah object at 0x1028ba490>
<method-wrapper '__init__' of Blah object at 0x1028ba490>
__main__
<built-in method __new__ of type object at 0x10276a4e0>
<built-in method __reduce__ of Blah object at 0x1028ba490>
<built-in method __reduce_ex__ of Blah object at 0x1028ba490>
<method-wrapper '__repr__' of Blah object at 0x1028ba490>
<method-wrapper '__setattr__' of Blah object at 0x1028ba490>
<built-in method __sizeof__ of Blah object at 0x1028ba490>
<method-wrapper '__str__' of Blah object at 0x1028ba490>
<built-in method __subclasshook__ of type object at 0x7fd522c6e490>

现在,您的里程数可能会因objc之类的东西而有所不同,因为它是一个很薄的Python包装器,用于进行共享库调用。它们不会有docstring,或者在某些情况下,如果函数查找是针对共享库的延迟查找,则会响应“dir”。但是,你永远不会知道。你知道吗

大多数时候,当涉及到objc的东西时,我只是在他们的源代码中挖掘,以找出当正常的挖土方法不起作用时他们是如何做的。你知道吗

说到常规方法:

Twisted的一个整洁的特性是,您还可以提供telnet或SSH可访问的交互式Python shell,它实际上可以“实时”地戳和戳东西。Check here for details on TwistedConch。你知道吗

或。。

另一个技巧是向对象添加一个“del(self)”函数,当对象被垃圾收集器清除时(当对象被删除/超出范围时),它会打印出一些内容

或。。

你也可以玩pdb,或者如果你喜欢ncurses的话pudb太棒了。看看这个问题,了解一些使用pdb的妙招。starting-python-debugger-automatically-on-error

如果情况越来越糟,你可以随时使用帮助(object)。你知道吗

这些基本上就是让我度过这一天的调试方法。如果其他人有一些聪明的想法,不要害羞。你知道吗

相关问题 更多 >