YouTip LogoYouTip

Python Exercise Example43

# Python2.x Python Exercise Instance 43 [![Image 3: Python 100 Examples](#) Python 100 Examples](#) **Title:** Another example of imitating static variables (static). **Program Analysis:** Demonstrates a method of using Python scope. Program source code: ## Instance #!/usr/bin/python # -*- coding: UTF-8 -*- class Num: nNum =1 def inc(self): self.nNum +=1 print('nNum = %d' % self.nNum) if __name__ =='__main__': nNum =2 inst = Num() for i in range(3): nNum +=1 print('The num = %d' % nNum) inst.inc() The output of the above instance is: The num = 3 nNum = 2The num = 4 nNum = 3The num = 5 nNum = 4 [
← Python Exercise Example44Python Exercise Example42 β†’