Normally, I would expect a function variable to be lost after execution of that function. In the below case where I wrote a Singleton following some tutorials, it works, though. One could argue that instancesshould be empty again everytime the singleton function is called.
def singleton(cls):
    instances = {}
    def get_instance(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return get_instance
@singleton
class cls(object):
    pass
My assumption is that indeed instances would be lost if it weren't the case that a dynamic function get_instanceis returned in which a reference to the variable is stored so that it is still available. This leads to the fact that instances continues existing.
Is that interpretation correct? Is it also correct that for every get_instance call there is a separate "instance" of instances? They cannot be the same as for every singleton call there is a fresh instances = {}.
 
    