YouTip LogoYouTip

Python Exercise Example41

# Python2.x Python Exercise Instance 41 [![Image 3: Python 100 Examples](#) Python 100 Examples](#) **Title:** Imitate the usage of static variables. **Program Analysis:** None. Program source code: ## Instance ```python #!/usr/bin/python # -*- coding: UTF-8 -*- def varfunc(): var = 0 print('var = %d' % var) var += 1 if __name__ == '__main__': for i in range(3): varfunc() # Class attribute # As a class attribute class Static: StaticVar = 5 def varfunc(self): self.StaticVar += 1 print(self.StaticVar) print(Static.StaticVar) a = Static() for i in range(3): a.varfunc() The output of the above instance is: var
← Python Exercise Example42Python Exercise Example40 β†’