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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23117717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:18:02  来源:igfitidea点击:

Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object

pythonpython-2.7inheritancesupernew-style-class

提问by cjm2671

I have the following Python 2.7 code:

我有以下 Python 2.7 代码:

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.

我正在尝试扩展该__init__()方法,以便当我实例化“Eye”时,除了 Frame 设置的内容之外,它还会执行其他一些操作(self.some_other_defined_stuff())。Frame.__init__()需要先运行。

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.

我不明白其中的逻辑原因。有人可以解释一下吗?我习惯于在 ruby​​ 中输入“super”。

采纳答案by Martijn Pieters

There are two errors here:

这里有两个错误:

  1. super()only works for new-style classes; use objectas a base class for Frameto make it use new-style semantics.

  2. You still need to call the overridden method with the right arguments; pass in imageto the __init__call.

  1. super()仅适用于新型类;使用object的基类Frame,使之使用新型语义。

  2. 您仍然需要使用正确的参数调用被覆盖的方法;传递image__init__通话。

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()

回答by myusuf3

Framemust extend objectbecause only the new style classes support supercall you make in Eyelike so:

Frame必须扩展,object因为只有新样式类支持superEye像这样调用:

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()

回答by Arjun Sharma

Hi see my working codes for python 2.7

嗨,请参阅我的 python 2.7 工作代码

__metaclass__ = type
class Person:
    def __init__(self, first, last, age):
        self.firstname = first
        self.lastname = last
        self.age = age

    def __str__(self):
        return self.firstname + " " + self.lastname + ", " + str(self.age)

class Employee(Person):
    def __init__(self, first, last, age, staffnum):
        super(Employee, self).__init__(first, last, age)
        self.staffnumber = staffnum

    def __str__(self):
        return super(Employee, self).__str__() + ", " +  self.staffnumber


x = Person("Marge", "Simpson", 36)
y = Employee("Homer", "Simpson", 28, "1007")

print(x)
print(y)

回答by ashwin r

Please write :__metaclass__ = typeat the top of the code then we can Access super class

请写:__metaclass__ = type在代码的顶部然后我们可以访问超类

__metaclass__ = type
class Vehicle:
                def start(self):
                                print("Starting engine")
                def stop(self):
                                print("Stopping engine")                            
class TwoWheeler(Vehicle):
                def say(self):
                    super(TwoWheeler,self).start()
                    print("I have two wheels")
                    super(TwoWheeler,self).stop()                            
Pulsar=TwoWheeler()
Pulsar.say()