creating accumulator function (python) -


how create accumulator function called repeatedly single numeric argument , accumulate argument sum. each time called, returns accumulated sum. example given below.

a=make_accumulator() a(10) -> 10 a(20) -> 30 a(-15) -> 15  

class myaccumulator:     def __init__(self):         self.sum = 0     def add(self, number):          self.sum += number         return self.sum  = myaccumulator().add print(a(10))      # => 10 print(a(20))      # => 30 print(a(-15))     # => 15 

something ?


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -