python继承super__init___当父不从对象继承时,Python 2.x super __init__继承不起作用

I have the following Python 2.7 code:

class Frame:

def __init__(self, image):

self.image = image

class Eye(Frame):

def __init__(self, image):

super(Eye, self).__init__()

self.some_other_defined_stuff()

I'm trying to extend the __init__() method so that when I instantiate an 'Eye' it does a bunch of other stuff (self.some_other_defined_stuff()), in addition to what Frame sets up. Frame.__init__() needs to run first.

I get the following error:

super(Eye, self).__init__()

TypeError: must be type, not classobj

Which I do not understand the logical cause of. Can someone explain please? I'm used to just typing 'super' in ruby.

解决方案

There are two errors here:

super() only works for new-style classes; use object as a base class for Frame to make it use new-style semantics.

You still need to call the overridden method with the right arguments; pass in image to the __init__ call.

So the correct code would be:

class Frame(object):

def __init__(self, image):

self.image = image

class Eye(Frame):

def __init__(self, image):

super(Eye, self).__init__(image)

self.some_other_defined_stuff()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值