使用超级 [英] using super

查看:85
本文介绍了使用超级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用超级但不断收到以下错误


E:\Code\Python\Nexus \Player> python ./super_test.py

回溯(最近一次调用最后一次):

文件" ./ super_test.py",第14行,在?

main()

文件" ./ super_test.py",第11行,in main

b = B()

文件" ./ super_test。 py",第7行,在__init__

super(B,self).__ init()

TypeError:super()参数1必须是type,而不是classobj


这里是代码:

A级:

def __init __(个体经营):

打印''A''


B级(A):

def __init __(个体经营):

super(B,self ).__ init()

打印''B''


def main():

b = B()


if __name__ ==''__ main__'':

main()

解决方案

< blockquote> sashan写道:

我正在尝试使用超级但仍然收到以下错误

E:\ Code = \\ Python \Nexus \Player> python ./super_test.py
回溯(最近一次调用最后一次):
文件./ super_test.py,第14行,在?
main()
文件./ super_test.py,第11行,主要是
b = B()
文件" ./ super_test.py",第7行,在__init__
super(B,self).__ init()
TypeError :super()参数1必须是type,而不是classobj

这里是代码:
class A:
def __init __(self):
print' 'A''
B级(A):
def __init __(自我):
超级(B,自我).__ init()
打印''B''
def main():
b = B()
如果__name__ ==''__ main__'':
main()




super()仅适用于新风格类,即扩展的类。
Python的对象类型。 A级(对象)应该可以解决你的问题。


干杯,马特


-

Matt Goodall, Pollenation Internet Ltd

w: http://www.pollenation.net

e: ma**@pollenation.net


>这是代码:


你还需要纠正:

super(B,self).__ init()

读取

super(B,self).__ init __()

A类:
def __init __(self) :
打印''A''

B类(A):
def __init __(自我):
super(B,self).__ init()< ;<< ====
打印''B''

def main():
b = B()

如果__name__ ==''__ main__'':
main()




I''m trying to use super but keep getting the following error

E:\Code\Python\Nexus\Player>python ./super_test.py
Traceback (most recent call last):
File "./super_test.py", line 14, in ?
main()
File "./super_test.py", line 11, in main
b = B()
File "./super_test.py", line 7, in __init__
super(B, self).__init()
TypeError: super() argument 1 must be type, not classobj

Here''s the code:
class A:
def __init__(self):
print ''A''

class B(A):
def __init__(self):
super(B, self).__init()
print ''B''

def main():
b = B()

if __name__ == ''__main__'':
main()

解决方案

sashan wrote:

I''m trying to use super but keep getting the following error

E:\Code\Python\Nexus\Player>python ./super_test.py
Traceback (most recent call last):
File "./super_test.py", line 14, in ?
main()
File "./super_test.py", line 11, in main
b = B()
File "./super_test.py", line 7, in __init__
super(B, self).__init()
TypeError: super() argument 1 must be type, not classobj

Here''s the code:
class A:
def __init__(self):
print ''A''
class B(A):
def __init__(self):
super(B, self).__init()
print ''B''
def main():
b = B()
if __name__ == ''__main__'':
main()



super() only works for "new style" classes, that is classes that extend
Python''s object type. class A(object) should solve your problem.

Cheers, Matt

--
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: ma**@pollenation.net


> Here''s the code:

You will also need to correct:
super(B, self).__init()
to read
super(B, self).__init__()

class A:
def __init__(self):
print ''A''

class B(A):
def __init__(self):
super(B, self).__init() <<<====
print ''B''

def main():
b = B()

if __name__ == ''__main__'':
main()




这篇关于使用超级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆