Latest web development tutorials

Pythonは演習43と、

Pythonの100リー Pythonの100リー

タイトル:模造静的変数(静的)別のケース。

プログラム解析: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

Pythonの100リー Pythonの100リー