python nameerror 名称未定义
python namerror name is not defined
我无法弄清楚为什么会出现此错误。我的代码是这样
#define the Animal Class
class Animal:
def __init__ (self, animal_type, age, color):
self.animal_type = animal_type
self.age = age
self.color = color
def makeNoise():
pass
def __str__(self):
print ("% is % years old and is %" % animal_type,age, color)
#define child classes of Animal
class Wolves(Animal):
def __init__(self, animal_type, age, color, wild):
Animal.__init__(self, animal_type, age, color)
self.wild = wild
def __str__(self):
print ("% is % years old and is % and is %" % (animal_type, age, color, wild))
class Bear(Animal):
def __init__ (self, animal_type, age, color, sex):
self.sex = sex
Animal.__init__(self,animal_type, age, color)
class Moose(Animal):
def __init__(self, animal_type, age, color, antlers):
self.antlers = antlers
Animal.__init__(self, animal_type, age, color)
#add items to each class
wally = Wolves("wolf", 4, "grey","wild")
sally = Wolves("wolf", 3, "white", "tame")
print (str(sally))
print (str(wally))
完整的回溯是
Traceback (most recent call last):
File "//mgroupnet.com/RedirectedFolders/SBT/Documents/bear51.py", line 41, in <module>
print (str(sally))
File "//mgroupnet.com/RedirectedFolders/SBT/Documents/bear51.py", line 24, in __str__
print ("% is % years old and is % and is %" % (animal_type, age, color, wild))
NameError: name 'animal_type' is not defined
我做错了什么?
哦 - 基本上你只是忘了在你的 __str__
方法中使用 self.animal_type
。像这样:
def __str__(self):
print ("%s is %s years old and is %s" % self.animal_type,self.age, self.color)
就像在 __init__
中一样,要使用实例化 class 中的变量,您需要使用 "self",如 "from this animal instance that I'm working on".
在 Python 中,方法只是普通函数。因此,您不能从一个方法中访问另一个方法中的局部变量。在方法之间共享信息的典型方式是通过 self
。要在 __str__
中获得 animal_type
,您需要使用 self.animal_type
。 class 中的方法没有特殊名称 space。这意味着就名称的可见性而言,无论您是在模块中编写函数还是在 class.
中编写方法都没有关系
在 Python 中,self
不是像 Java 中的 this
这样的关键字。它只是一个与其他任何参数一样的参数,按照惯例,通常称为 self
.
当你调用一个方法时,比如some_animal.__str__()
1,这实际上只是Animal.__str__(some_animal)
的语法糖,其中some_animal
被绑定到 self
参数。
因此,在 Java(和许多其他语言)中,this
表示 "look at the current instance for this attribute" 并且是可选的,当明确时(即当没有同名的局部变量时),但是 Python 的 self
不是可选的。这只是一个常规的方法参数。
1 __str__
不是一个很好的例子,因为你从来没有这样称呼它,而是 str(some_animal)
,但你知道我的意思
我无法弄清楚为什么会出现此错误。我的代码是这样
#define the Animal Class
class Animal:
def __init__ (self, animal_type, age, color):
self.animal_type = animal_type
self.age = age
self.color = color
def makeNoise():
pass
def __str__(self):
print ("% is % years old and is %" % animal_type,age, color)
#define child classes of Animal
class Wolves(Animal):
def __init__(self, animal_type, age, color, wild):
Animal.__init__(self, animal_type, age, color)
self.wild = wild
def __str__(self):
print ("% is % years old and is % and is %" % (animal_type, age, color, wild))
class Bear(Animal):
def __init__ (self, animal_type, age, color, sex):
self.sex = sex
Animal.__init__(self,animal_type, age, color)
class Moose(Animal):
def __init__(self, animal_type, age, color, antlers):
self.antlers = antlers
Animal.__init__(self, animal_type, age, color)
#add items to each class
wally = Wolves("wolf", 4, "grey","wild")
sally = Wolves("wolf", 3, "white", "tame")
print (str(sally))
print (str(wally))
完整的回溯是
Traceback (most recent call last):
File "//mgroupnet.com/RedirectedFolders/SBT/Documents/bear51.py", line 41, in <module>
print (str(sally))
File "//mgroupnet.com/RedirectedFolders/SBT/Documents/bear51.py", line 24, in __str__
print ("% is % years old and is % and is %" % (animal_type, age, color, wild))
NameError: name 'animal_type' is not defined
我做错了什么?
哦 - 基本上你只是忘了在你的 __str__
方法中使用 self.animal_type
。像这样:
def __str__(self):
print ("%s is %s years old and is %s" % self.animal_type,self.age, self.color)
就像在 __init__
中一样,要使用实例化 class 中的变量,您需要使用 "self",如 "from this animal instance that I'm working on".
在 Python 中,方法只是普通函数。因此,您不能从一个方法中访问另一个方法中的局部变量。在方法之间共享信息的典型方式是通过 self
。要在 __str__
中获得 animal_type
,您需要使用 self.animal_type
。 class 中的方法没有特殊名称 space。这意味着就名称的可见性而言,无论您是在模块中编写函数还是在 class.
在 Python 中,self
不是像 Java 中的 this
这样的关键字。它只是一个与其他任何参数一样的参数,按照惯例,通常称为 self
.
当你调用一个方法时,比如some_animal.__str__()
1,这实际上只是Animal.__str__(some_animal)
的语法糖,其中some_animal
被绑定到 self
参数。
因此,在 Java(和许多其他语言)中,this
表示 "look at the current instance for this attribute" 并且是可选的,当明确时(即当没有同名的局部变量时),但是 Python 的 self
不是可选的。这只是一个常规的方法参数。
1 __str__
不是一个很好的例子,因为你从来没有这样称呼它,而是 str(some_animal)
,但你知道我的意思