【问题标题】:Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object当父级不从对象继承时,Python 2.x 超级 __init__ 继承不起作用
【发布时间】:2014-05-31 19:32:45
【问题描述】:

我有以下 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()

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

我收到以下错误:

super(Eye, self).__init__()
TypeError: must be type, not classobj

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

【问题讨论】:

  • Frame 必须扩展 objectsuper 仅适用于新型类。

标签: python python-2.7 inheritance super new-style-class


【解决方案1】:

请在代码顶部写上:__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()

【讨论】:

    【解决方案2】:

    您好,请参阅我的 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)
    

    【讨论】:

    • 要改进此答案,请检查您的格式并为您的代码提供解释。
    【解决方案3】:

    Frame 必须扩展 object,因为只有新样式类支持您在 Eye 中进行的 super 调用,如下所示:

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

    【讨论】:

      【解决方案4】:

      这里有两个错误:

      1. super() 仅适用于new-style classes;使用object 作为Frame 的基类,使其使用新式语义。

      2. 您仍然需要使用正确的参数调用被覆盖的方法;将image 传递给__init__ 调用。

      所以正确的代码是:

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

      【讨论】:

      • 在 Python > 3.X 中对object 的引用是否是多余的?
      • @gented: 是的,object 作为基类隐含在 Python 3 中(因为不再有旧式类)。
      猜你喜欢
      • 2012-05-16
      • 2016-08-28
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 2011-09-26
      • 2011-06-17
      • 1970-01-01
      相关资源
      最近更新 更多