我是一個新的Python程序學習者,並且我將它創建爲下面的類實例時感到困惑,它們有什麼不同?這兩種方法對新的python類實例有什麼區別
類聲明:
class TestClass:
def __init__(self, one=10, two=20, three=30):
self.one = one
self.two = two
self.three = three
1日。 (撥錯)
x = TestClass
print x
print x(50)
print x.one, x.two
輸出:
__main__.TestClass
<__main__.TestClass instance at 0x0000000002445208>
Traceback (most recent call last):
File "D:\workspace-QATool_vNext\testStudyCode\test\StudyCode.py", line 27, in <module>
print x.one, x.two
AttributeError: class TestClass has no attribute 'one'
第二。 (更正)
y = TestClass()
print y
print y.one, y.two
輸出:
<__main__.TestClass instance at 0x00000000023B5208>
10 20
非常感謝,我現在明白了,並更新了我的代碼,它的工作原理: – user2538491
不客氣。也許這個[page](http://docs.python.org/2/tutorial/classes.html)也是有用的。 – RickyA
非常感謝。 – user2538491