Why in the output of this script, the integer 18 will be displayed three times? It shouldn’t be two times? one time for executing the function and one for printing the return value?
<code>#! /usr/bin/python3
class c:
def __init__ (self,func):
self.cache = {}
self.func = func
def __call__ (self,*args):
if args not in self.cache:
self.cache[args] = self.func(*args)
print (self.cache[args])
return self.cache[args]
@c
def f1(a):
return a*2
print(c(f1)(9))type here
</code>
<code>#! /usr/bin/python3
class c:
def __init__ (self,func):
self.cache = {}
self.func = func
def __call__ (self,*args):
if args not in self.cache:
self.cache[args] = self.func(*args)
print (self.cache[args])
return self.cache[args]
@c
def f1(a):
return a*2
print(c(f1)(9))type here
</code>
#! /usr/bin/python3
class c:
def __init__ (self,func):
self.cache = {}
self.func = func
def __call__ (self,*args):
if args not in self.cache:
self.cache[args] = self.func(*args)
print (self.cache[args])
return self.cache[args]
@c
def f1(a):
return a*2
print(c(f1)(9))type here
I expected that the output display two times.
New contributor
Red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.