Can a Python function remember its previous outputs? [duplicate]

若如初见. 提交于 2021-02-11 04:22:14

问题


Is there a way that a function can remember its previous output and use that value during the next call to the function? For instance, assume there is a function, runningTotal with a single argument x that returns x on the first call to runningTotal but x + prevOutput for every call after that. Is there a way to write such a function in python?

I am aware that this could be easily achieved by using a global variable in the function or by saving the previous value to a new variable, but I would like to avoid these solutions if possible. The reason I'm looking for an alternate solution is because this is one function in a program I'm working on with other people and I would like to avoid having to create more global variables than already established.


回答1:


Although there are ways of doing what you ask, it's not a good idea. As @JohnColeman pointed out, Simulate static variables in python with closures

But why not create a class?

class Accumulator:
    total = 0

    @classmethod
    def add(cls, x):
        cls.total += x
        return cls.total


print(Accumulator.add(1))
print(Accumulator.add(2))
print(Accumulator.add(3))

Result:

1
3
6

You can set up a generator to maintain state and send values to it as well, as suggested by @HeapOverflow:

def get_running_total():
    def _running_total():
        value = 0
        while True:
            value += yield value
    # get a generator instance
    generator = _running_total()
    # set it up to wait for input
    next(generator)
    # return the send method on the generator
    return generator.send


# you can get a generator that functions similar to the Accumulator method
running_total = get_running_total()
print(running_total(1))   # prints 1
print(running_total(2))   # prints 3
print(running_total(3))   # prints 6



回答2:


Yes, but to avoid too much hackery or GLOBAL variables we'll probably want to use a class. In python a class can be treated as function with a magic function (method) inside the class named __call__.

Your question might be better written: what's the best way to have a function in python that has internal state?

Let's say we have the runningTotal function defined using a global variable as:

TOTAL = 0
def runningTotal(inc):
    global TOTAL
    TOTAL += inc
    return TOTAL

Answer Ok so lets define a class that will behave the same way as the above function but without a global variable:


class StatefulFunction:
    running_total = 0

    def __call__(self, inc):
        self.running_total += inc
        return self.running_total


# create the stateful function variable
runningTotal = StatefulFunction()

# Use the stateful function
runningTotal(1)
# outputs: 1
runningTotal(5)
# outputs: 6

Another way to accomplish the same thing is with a Counter Dictionary

from collections import Counter
counter = Counter()
counter['runningTotal'] += 1
# in another part of the program
counter['runningTotal'] += 5

The output will be:

print(counter)
Counter({'runningTotal': 6})


来源:https://stackoverflow.com/questions/59674321/can-a-python-function-remember-its-previous-outputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!