当父级不从对象继承时,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
Python 2.x super __init__ inheritance doesn't work when parent doesn't inherit from object
提问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:
这里有两个错误:
super()
only works for new-style classes; useobject
as a base class forFrame
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.
super()
仅适用于新型类;使用object
的基类Frame
,使之使用新型语义。您仍然需要使用正确的参数调用被覆盖的方法;传递
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
Frame
must extend object
because only the new style classes support super
call you make in Eye
like so:
Frame
必须扩展,object
因为只有新样式类支持super
您Eye
像这样调用:
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__ = type
at 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()