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

Context for me is the only value of information that I need to keep between calls to a function that changes this value. I could use global, but I know that discouraged. At the moment, I used the default argument in the form of a list containing int, and took advantage of the variability so that changes in the value persist between calls, for example:

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

This function attaches to the button via tkinter, for example ~ ~ //

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

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

How do people usually deal with this? I mean, of course, the trick with variability works and that’s it, but what if I need to access and change this value from several functions?

..?

+4
4

. .

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

__call__ (, ). , .

..?

, , / , "pythonic". python ( 5 ), .

python mutable-default-args "weirdness" ( "" ) .

+5

?

, , . , (.. ), , , .

(UI), , , .

, , singleton, ( ' , singleton - ):

def singleton(cls):
    instance = cls()
    instance.__call__ = lambda: instance
    return instance

@singleton
class TheDataModel(object):
    def __init__(self):
        self.x = 0

    def on_button_demo(self):
        self.x += 1

if __name__ == '__main__':
    # If an element needs a reference to the model, just get
    # the current instance from the decorated singleton:
    model = TheDataModel
    print('model', model.x)
    model.on_button_demo()
    print('model', model.x)

    # In fact, it is a global instance that is available via
    # the class name; even across imports in the same session
    other = TheDataModel
    print('other', other.x)

    # Consequently, you can easily bind the model methods
    # to the action of any UI element
    button0 = Button(root, text="demo", command=TheDataModel.on_button_demo)

, , singleton, . singleton. .

+1

, 1 - . , mainloop, , .

:

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

def increment_and_save(val):
    global saved
    saved = increment(val)

1 , , , , .

+1

, context managers. , .

class MyContext(object):
    # This is my container 
    # have whatever state to it
    # support different operations

    def __init__(self):
        self.val = 0

    def increament(self, val):
        self.val += val

    def get(self):
        return self.val

    def __enter__(self):
        # do on creation
        return self


    def __exit__(self, type, value, traceback):
        # do on exit
        self.val = 0

def some_func(val, context=None):
    if context:
        context.increament(val)

def some_more(val, context=None):
    if context:
        context.increament(val)

def some_getter(context=None):
    if context:
        print context.get()

with MyContext() as context:
    some_func(5, context=context)
    some_more(10, context=context)
    some_getter(context=context)
0

Source: https://habr.com/ru/post/1668706/


All Articles