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

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -