Python AssertionError:None是不可调用的

Python AssertionError:None是不可调用的,python,twisted,Python,Twisted,我正在学习如何使用twisted编程,并正在学习Dave Peticolas的教程()。我正在尝试解决第3部分末尾建议的练习-在countdown.py上进行多个独立的倒计时。这是我的代码和我得到的错误: #!/usr/bin/python class countdown(object): def __init__(self): self.timer = 0 def count(self, timer): if self.timer == 0

我正在学习如何使用twisted编程,并正在学习Dave Peticolas的教程()。我正在尝试解决第3部分末尾建议的练习-在countdown.py上进行多个独立的倒计时。这是我的代码和我得到的错误:

#!/usr/bin/python

class countdown(object):

    def __init__(self):
        self.timer = 0

    def count(self, timer):
        if self.timer == 0:
            reactor.stop()
        else:
            print self.timer, '...'
            self.timer -= 1
            reactor.callLater(1, self.count)


from twisted.internet import reactor

obj = countdown()
obj.timer = 10
reactor.callWhenRunning(obj.count(obj.timer))

print 'starting...'
reactor.run()
print 'stopped.'
执行时:

$ ./countdown.py
10 ...
Traceback (most recent call last):
  File "./countdown.py", line 21, in <module>
    reactor.callWhenRunning(obj.count(obj.timer))
  File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 666, in callWhenRunning
    _callable, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 645, in addSystemEventTrigger
    assert callable(_f), "%s is not callable" % _f
AssertionError: None is not callable
$./countdown.py
10 ...
回溯(最近一次呼叫最后一次):
文件“/countdown.py”,第21行,在
反应堆运行时调用(对象计数(对象计时器))
文件“/usr/lib/python2.7/dist packages/twisted/internet/base.py”,第666行,在运行时调用
_可调用,*args,**kw)
addSystemEventTrigger中的文件“/usr/lib/python2.7/dist packages/twisted/internet/base.py”,第645行
断言可调用(\u f),%s不可调用“%\u f
AssertionError:None是不可调用的

我假设我在利用对象变量时没有做正确的事情;虽然我不确定我做错了什么。

在传递之前,您称您可呼叫
obj.count()
调用返回的结果不可调用

您需要传入方法,而不是调用它的结果:

reactor.callWhenRunning(obj.count, (obj.timer,))
方法的位置参数(此处仅为
obj.timer
)应作为单独的元组提供

仔细检查,您甚至不需要将
obj.timer
作为参数传入。您只需在
self
上访问它,毕竟,不需要单独传递它:

class countdown(object):
    def __init__(self):
        self.timer = 0

    def count(self):
        if self.timer == 0:
            reactor.stop()
        else:
            print self.timer, '...'
            self.timer -= 1
            reactor.callLater(1, self.count)
并在运行()时相应地调整您的
调用。
调用:

reactor.callWhenRunning(obj.count)

在传入之前,您称自己为可调用的
obj.count()
调用返回的结果不可调用

您需要传入方法,而不是调用它的结果:

reactor.callWhenRunning(obj.count, (obj.timer,))
方法的位置参数(此处仅为
obj.timer
)应作为单独的元组提供

仔细检查,您甚至不需要将
obj.timer
作为参数传入。您只需在
self
上访问它,毕竟,不需要单独传递它:

class countdown(object):
    def __init__(self):
        self.timer = 0

    def count(self):
        if self.timer == 0:
            reactor.stop()
        else:
            print self.timer, '...'
            self.timer -= 1
            reactor.callLater(1, self.count)
并在运行()时相应地调整您的
调用。
调用:

reactor.callWhenRunning(obj.count)

谢谢你的快速回复。这就解决了我的问题。我希望有两个不同的计时器对象;尽管这只会导致反应堆在第一个计时器超时后停止。实现这一点的最佳方式是什么?感谢您的快速响应。这就解决了我的问题。我希望有两个不同的计时器对象;尽管这只会导致反应堆在第一个计时器超时后停止。实现这一目标的最佳方式是什么?