蒐集

鮮やかな憧憬

pythonのクラスとインスタンス

ちょっと大きいコードを触った時に?となったのでメモ

% python [14:18:56]
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
... count = 0
... def hello(self):
... print("Hello {0}".format(self.count))
... self.count += 1
...
>>> a1 = A()
>>> a2 = A()
>>> a1.hello()
Hello 0
>>> a1.hello()
Hello 1
>>> a1.hello()
Hello 2
>>> a1.hello()
Hello 3
>>> a1.hello()
Hello 4
>>> a1.hello()
Hello 5
>>> a1.hello()
Hello 6

>>> a2.hello()
Hello 0