如何使用超级命令python
我在使用超级命令时遇到问题如何使用超级命令python,python,super,Python,Super,我在使用超级命令时遇到问题 class s: def __init__(self,a): self.a=a def show(self): print self.a class t: def __init__(self,b): self.b=b def show2(self): print self.b class w(s): def __init__(self,a,b,c):
class s:
def __init__(self,a):
self.a=a
def show(self):
print self.a
class t:
def __init__(self,b):
self.b=b
def show2(self):
print self.b
class w(s):
def __init__(self,a,b,c):
super(w,self).__init__()
self.b=b
self.c=c
def show3(self):
super(w,self).show()
print self.b
print self.c
每当我创建一个对象时,它都会给出以下错误
x=w(1,2,3)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
x=w(1,2,3)
File "C:\Users\GURSAHEJ\Desktop\k.py", line 13, in __init__
super(w,self).__init__()
TypeError: must be type, not classobj
x=w(1,2,3)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
x=w(1,2,3)
文件“C:\Users\GURSAHEJ\Desktop\k.py”,第13行,在\uu init中__
super(w,self)。\uuuu init\uuuuu()
TypeError:必须是type,而不是classobj
将返回一个代理对象,该对象将方法调用委托给类型为的父类或同级类,因此当您要使用它时,需要将父类名称传递给它,并且由于您的w
类继承自s
,因此您可能希望将s
传递给super
:
class w(s):
def __init__(self,a,b,c):
super(s,self).__init__()
self.b=b
self.c=c
另外,不要忘记将传递给父类,使其成为:
请在python文档中阅读有关新样式和经典类的信息,因为您正在一个平台上使用
super
在Python2.x(>=2.2)中,有两种类型的类。老式班和新式班。在Python3.x中,旧样式的类被删除,所有的类都是新样式的类
Python的内置函数super
可以在新样式的类上正常工作
简单地说,新样式的类扩展了object
,而旧样式的类没有扩展
老赛特班
新型课堂
因此,没有从另一个类继承的类应该从object
继承,成为一个新样式的类
您可以使用新样式或旧的skool继承\uuuu init\uuuu
class w(s):
def __init__(self,a,b,c):
s.__init__(self, a)
这是一个示例,但我不会将此问题标记为重复问题,因为鼓励使用新样式的类,并且应该避免使用旧样式的类
class s:
def __init__(self,a):
self.a=a
class s(object):
def __init__(self,a):
self.a=a
class w(s):
def __init__(self,a,b,c):
s.__init__(self, a)