|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random as r
class Fish:
def __init__(self):
self.x = r.randint(0,10)
self.y = r.randint(0,10)
def move(self):
self.x -=1
print '我的位置:' ,self.x,self.y
class Shark(Fish):
def __init__(self):
#Fish.__init__(self)
super(Shark,self).__init__() ##python2这样使用super会报错,错误提示下面给出了。
#super().__init__() ##python3中可以这样使用super不报错,程序能正常运行
self.hungry = True
if self.hungry:
print '好吃'
self.hungry = False
else:
print 'so much'
查了下help文档,应该python2中的super方法是这样使用的,但是执行过程中会报错,有谁知道python2中super方法的具体用法么。这个例子里应该如何修改代码,谢谢哈!
我使用的版本是python2.7.8
D:\python_script>python
Python 2.7.8 (default, Jun 30 2014, 16:08:48)
D:\python_script>python fish.py
我的位置: 9 2
我的位置: 4 10
Traceback (most recent call last):
File "fish.py", line 39, in <module>
shark = Shark()
File "fish.py", line 25, in __init__
super(Shark,self).__init__()
TypeError: must be type, not classobj
已解决,在第一个class里加上object即可
class Fish(object)
|
|