在python2中的方法内部调用的超级函数

2024-09-28 01:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试我的超级函数,下面是我正在执行的代码。你知道吗

class scene(object):
    def enter(self):
        print "a vllan s n your way. what you'll do?"

class centralcorrdor(scene):
    print "startng pont of the game."
    super(centralcorrdor,self).enter()


a = centralcorrdor()

但是这会产生错误。你知道吗

class centralcorrdor(scene):
File "game.py", line 8, in centralcorrdor
super(centralcorrdor,self).enter()

NameError: name 'centralcorrdor' is not defined

但事实并非如此。你知道吗

class scene(object):
    def enter(self):
        print "a vllan s n your way. what you'll do?"

class centralcorrdor(scene):
    #print "startng pont of the game."
    def func(self):
        super(centralcorrdor,self).enter()
    #scene.enter()

a = centralcorrdor()
a.func()

有人能告诉我为什么吗?是不是在子类的方法内部调用了super?你知道吗


Tags: selfyougameyourobjectdefscenewhat
2条回答

必须在方法中使用super。有关更多信息,请咨询: python-programmingusing-super-with-a-class-method 我希望这对你有帮助;)

super实际上是基类的代理。有类代理(在静态或类方法中定义)和实例代理(在实例方法中定义)。你知道吗

您的super(centralcorrdor,self).enter()语句传递self作为对象参数,如果您选中enter签名就是self。因此,必须在实例对象上调用它,而不是在类方法中。你知道吗

一般来说,大多数时候您都会调用super(class, object)。你知道吗

相关问题 更多 >

    热门问题