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

前端 未结 26 2742
天命终不由人
天命终不由人 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:26

    Soulution n +=1

    def foo():
      foo.__dict__.setdefault('count', 0)
      foo.count += 1
      return foo.count
    

提交回复
热议问题