[python] static variable in function

spring·2020년 11월 9일

C에서처럼 함수 내부에 static 변수를 만들어서 불필요한 초기화 또는 생성을 막는 방법이다.

def foo():
    if 'count' not in foo.__dict__:
        foo.count = 0
    foo.count += 1
    return foo.count


print(foo())
print(foo())
1
2
profile
Researcher & Developer @ NAVER Corp | Designer @ HONGIK Univ.

0개의 댓글