I was practicing writing a class and ended up with this problem. I wrote a class that should return the value of the function after 5th instance is generated and do nothing for instances less than 5. I wrote the below code, but the instance count is not added every time I create an instance. The display in __init__
shows 1 for the first instance and after that the display is not there.. I would like to know what is that I am missing. I just felt like __init__
is not called after the first instance call. I checked by removing the 'IF' condition in the __call__
and every time the result of the function 10 is displayed but count is not increasing.
class After5(object):
call_count = 0
def __init__(self, funct):
After5.call_count += 1
print('Count{0}'.format(After5.call_count))
self.funct = funct
def __call__(self):
if After5.call_count > 5: #enter code here
res = self.funct()
print('{0}'.format(res))
print('Sending Results{0}'.format(After5.call_count))
return res
@After5
def dummy_funct():
return 5 * 2
dummy_funct()
dummy_funct()
dummy_funct()
dummy_funct()
dummy_funct()
dummy_funct()