Define two class based on BaseHandler
as below:
class BaseHandler:
def successor(self, successor):
self.successor = successor
class ScoreHandler1(BaseHandler):
pass
class ScoreHandler2(BaseHandler):
pass
Initialize two instances:
h1 = ScoreHandler1()
h2 = ScoreHandler2()
Call successor method first time :
h1.successor(h2)
Now call it second time:
h1.successor(h2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'ScoreHandler2' object is not callable
Why can't call the method more times?
callable(ScoreHandler2)
True
CodePudding user response:
The first time you call h1.successor(h2)
, you're calling the method named "successor". Inside this method, you set the attribute named "successor" to the object h2
.
The second time you call h1.successor(h2)
, you're calling the attribute named "successor", which you defined previously to h2
. Since ScoreHandler2
does not implement __call__
, it'll raise an error.
To fix this, avoid naming attributes with the same name as methods.