I am learning twisted and have developed a simple python script that should count down each letter of the alphabet and stop the reactor when it gets to z
. However, I am getting the following error:
assert builtins.callable(callable), f"{callable} is not callable" builtins.AssertionError: a is not callable
Here's my script:
class Countletters(object):
letters = list( map(chr, range(97, 123)) )
def countletters(self):
for l in self.letters:
if l == 'z':
rector.stop()
else:
print(l)
reactor.callLater(1, l)
from twisted.internet import reactor
reactor.callWhenRunning(Countletters().countletters)
print('Start countdown')
reactor.run()
print('Stop countdown')
You are almost there - the problem is the callLater
part.
callLater
expects a function, something "Callable", but you give it l
; a string (the current letter).
Try this for an example:
I pass functools.partial(print, l)
to callLater
which basically creates a callable object, which will simply do print(l)
when called.
import functools
from twisted.internet import reactor
class Countletters(object):
letters = list(map(chr, range(97, 123)))
def countletters(self):
for l in self.letters:
if l == 'z':
reactor.stop()
else:
print(l)
reactor.callLater(1, functools.partial(print, l))
reactor.callWhenRunning(Countletters().countletters)
print('Start countdown')
reactor.run()
print('Stop countdown')