下面的代码有语法错误。为什么歌曲不被视为一个属性?你知道吗
class MyStuff(object):
def _ini_(self):
self.song = "Hey Brother"
def apple(self):
print "I got a iphone"
music = MyStuff()
music.apple()
print music.song
错误:
I got a iphone
Traceback (most recent call last):
File "main.py", line 9, in
print music.song
AttributeError: 'MyStuff' object has no attribute 'song'
您将方法初始值设定项命名错误:
创建实例时不会自动调用。因此,永远不会创建
song
属性,以后尝试访问它会导致AttributeError
异常。你知道吗命名为
__init__
:注意单词
init
前后的双下划线。你知道吗演示:
相关问题 更多 >
编程相关推荐