- “No Module Named或者ImportError类的错误”,基本都是由于循环嵌套导致,建议把继承关系的子类和父类放到一个py文件里。 错误示例: 我们写两个类文件,一个父类room.py,一个子类goldroom.py
import goldroom
class Room(object):
#此处建立一个列表来模拟错误
rooms = [GoldRoom()]
def enter(self):
print "Enter the room"
- 2.goldroom.py
```
imort room
class GoldRoom.py
rooms = [Room()]
def enter(self):
print "Enter the GoldRoom."
- “take no arguments”错误,由于类方法定义缺少‘self’参数导致程序报错。 错误示例: 编写一个空参数的python方法。
class Test(object):
def test():
print "Test none argument"
test = Test()
test.test()
3. “UnboundLocalError: local variable referenced before assignment”和“Global name is not defined”类错误,这种错误主要是因为全局变量和局部变量区分不清导致,当我们在函数里使用全局变量时,需要加上self前缀,而使用局部变量时需要初始化局部变量。
错误示例:
- Test.py
```
class Test(object):
items = [1,1,1,2]
def test(self):
for t in items:
print "number is %d" % t
test = Test()
test.test()