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

一个人想着一个人 提交于 2019-11-27 14:38:56

问题


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.


回答1:


There are two errors here:

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

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



回答2:


Frame must extend object because only the new style classes support super call you make in Eye like so:

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



回答3:


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

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



回答4:


Hi see my working codes for 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)


来源:https://stackoverflow.com/questions/23117717/python-2-x-super-init-inheritance-doesnt-work-when-parent-doesnt-inherit-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!