python错误名称未定义错误名称
python错误名称未定义错误名称,python,nameerror,Python,Nameerror,name错误:未定义名称“def_init_” 我尝试在“init”的两侧使用2个下划线这看起来像是一个简单的打字错误。您缺少def后面的空格-应该是: class Bird : '''A base class to define bird properties.''' count = 0 def_init_( self , chat ) self.sound = chat Bird.count += 1 def talk( self ) :
name错误:未定义名称“def_init_”
我尝试在“init”的两侧使用2个下划线这看起来像是一个简单的打字错误。您缺少def
后面的空格-应该是:
class Bird :
'''A base class to define bird properties.'''
count = 0
def_init_( self , chat )
self.sound = chat
Bird.count += 1
def talk( self ) :
return self.sound
实际上,你的脚本中有很多错误
1> 压痕
2> 在def后留出空间
3> definit之后的冒号(self,chat)
遵守规则,一切都会好起来的
def __init__(self, chat):
您需要将\u init\u
函数更改为def\u init\u(self,chat):
\uuu
称为dunder,是“双下划线”的缩写。我们通常使用这种神奇的方法,比如实例化。因此,例如,[]
将被称为\uuu get\uu
每当你看到dunder,你就知道它有特殊的用途。def\u init\u
不是函数或变量。修复它(但是是适当的)。并且缺少:
和缩进..是的,在上面的代码中缩进是非常错误的。不过,我从不确定其中有多少是真实的,有多少是复制/粘贴错误。
class Bird :
'''A base class to define bird properties.'''
count = 0
def __init__( self , chat ):
self.sound = chat
Bird.count += 1
def talk( self ):
return self.sound