>>> C = Residues()
>>> C.atoms.append('thing')
Traceback (most recent call last):
File "<pyshell#84>", line 1, in <module>
B.atoms.append('thing')
AttributeError: Residues instance has no attribute 'atoms'
>>> C.setdata('something')
>>> C.atoms.append('thing') # now it works
>>>
您的类没有
__init__()
,因此在实例化时,属性atoms
不存在。你必须做C.setdata('something')
这样C.atoms
就可以使用了。与Java等语言不同,在Java语言中,您在编译时知道对象将具有哪些属性/成员变量,而在Python中,您可以在运行时动态添加属性。这也意味着同一类的实例可以有不同的属性。
为了确保您始终拥有一个
atoms
列表,您可以添加一个构造函数:相关问题 更多 >
编程相关推荐