What is the Python equivalent of static variables inside a function?

前端 未结 26 2793
天命终不由人
天命终不由人 2020-11-22 00:45

What is the idiomatic Python equivalent of this C/C++ code?

void foo()
{
    static int counter = 0;
    counter++;
          


        
26条回答
  •  遥遥无期
    2020-11-22 01:44

    Prompted by this question, may I present another alternative which might be a bit nicer to use and will look the same for both methods and functions:

    @static_var2('seed',0)
    def funccounter(statics, add=1):
        statics.seed += add
        return statics.seed
    
    print funccounter()       #1
    print funccounter(add=2)  #3
    print funccounter()       #4
    
    class ACircle(object):
        @static_var2('seed',0)
        def counter(statics, self, add=1):
            statics.seed += add
            return statics.seed
    
    c = ACircle()
    print c.counter()      #1
    print c.counter(add=2) #3
    print c.counter()      #4
    d = ACircle()
    print d.counter()      #5
    print d.counter(add=2) #7
    print d.counter()      #8    
    

    If you like the usage, here's the implementation:

    class StaticMan(object):
        def __init__(self):
            self.__dict__['_d'] = {}
    
        def __getattr__(self, name):
            return self.__dict__['_d'][name]
        def __getitem__(self, name):
            return self.__dict__['_d'][name]
        def __setattr__(self, name, val):
            self.__dict__['_d'][name] = val
        def __setitem__(self, name, val):
            self.__dict__['_d'][name] = val
    
    def static_var2(name, val):
        def decorator(original):
            if not hasattr(original, ':staticman'):    
                def wrapped(*args, **kwargs):
                    return original(getattr(wrapped, ':staticman'), *args, **kwargs)
                setattr(wrapped, ':staticman', StaticMan())
                f = wrapped
            else:
                f = original #already wrapped
    
            getattr(f, ':staticman')[name] = val
            return f
        return decorator
    

提交回复
热议问题