這段代碼我覺得很好的說明了python中類的方法在加self和不加self的區別。
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 | >>> classAAA(object):...     defgo(self):...         self.one ='hello'...>>> classBBB(object):...     defgo(self):...         one ='hello'...>>> a1 =AAA()>>> a1.go()>>> a1.one'hello'>>> a2 =AAA()>>> a2.oneTraceback (most recent call last):  File"<stdin>", line 1, in<module>AttributeError: 'AAA'objecthas no attribute 'one'>>> a2.go()>>> a2.one'hello'>>> b1 =BBB()>>> b1.go()>>> b1.oneTraceback (most recent call last):  File"<stdin>", line 1, in<module>AttributeError: 'BBB'objecthas no attribute 'one'>>> BBB.oneTraceback (most recent call last):  File"<stdin>", line 1, in<module>AttributeError: typeobject'BBB'has no attribute 'one'>>> classBBB(object):...     defgo(self):...         one ='hello'...         PRintone...         self.another =one...>>> b2 =BBB()>>> b2.go()hello>>> b2.another'hello'>>> b2.oneTraceback (most recent call last):  File"<stdin>", line 1, in<module>AttributeError: 'BBB'objecthas no attribute 'one' | 
新聞熱點
疑難解答