Python 練習實例43
題目:模仿靜態(tài)變量(static)另一案例。
程序分析:演示一個python作用域使用方法
程序源代碼:
#!/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 num = 3 nNum = 2 The num = 4 nNum = 3 The num = 5 nNum = 4
更多建議: