Python 练习实例43
python 练习实例43
题目:模仿静态变量(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