我是python程序的新学习者,我对创建类实例感到困惑,如下所示,它们有什么不同?
类声明:
class TestClass:
def __init__(self, one=10, two=20, three=30):
self.one = one
self.two = two
self.three = three
第一个。(穿着)
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