Search code examples
pythonvariablesstate

What is the pythonic way of saving data between function calls?


The context for me is a single int's worth of info I need to retain between calls to a function which modifies that value. I could use a global, but I know that's discouraged. For now I've used a default argument in the form of a list containing the int and taken advantage of mutability so that changes to the value are retained between calls, like so--

 def increment(val, saved=[0]):
    saved[0] += val
    # do stuff

This function is being attached to a button via tkinter, like so~

button0 = Button(root, text="demo", command=lambda: increment(val))

which means there's no return value I can assign to a local variable outside the function.

How do people normally handle this? I mean, sure, the mutability trick works and all, but what if I needed to access and modify that value from multiple functions?

Can this not be done without setting up a class with static methods and internal attributes, etc?


Solution

  • Use a class. Use an instance member for keeping the state.

    class Incrementable:
        def __init__(self, initial_value = 0):
            self.x = initial_value
        def increment(self, val):
            self.x += val
            # do stuff
    

    You can add a __call__ method for simulating a function call (e.g. if you need to be backward-compatible). Whether or not it is a good idea really depends on the context and on your specific use case.

    Can this not be done without setting up a class with static methods and internal attributes, etc?

    It can, but solutions not involving classes/objects with attributes are not "pythonic". It is so easy to define classes in python (the example above is only 5 simple lines), and it gives you maximal control and flexibility.

    Using python's mutable-default-args "weirdness" (I'm not going to call it "a feature") should be considered a hack.