Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 超类可以从单独的模块调用子类方法吗?_Python_Inheritance - Fatal编程技术网

Python 超类可以从单独的模块调用子类方法吗?

Python 超类可以从单独的模块调用子类方法吗?,python,inheritance,Python,Inheritance,如果类位于单独的模块中,超类能否调用子类方法“execute”?我知道如果他们在同一模块中,这可以工作 file2.py import file1 class TestCase(file1.TestBase): def execute(self): self._pass() file1.py class TestBase: def _pass(self): print "PASS" testBase = TestBase() testBas

如果类位于单独的模块中,超类能否调用子类方法“execute”?我知道如果他们在同一模块中,这可以工作

file2.py

import file1
class TestCase(file1.TestBase):

    def execute(self):
        self._pass()
file1.py

class TestBase:

    def _pass(self):
        print "PASS"

testBase = TestBase()
testBase.execute()

是的,但你必须明确地称之为:

from file1 import TestBase

class TestCase(TestBase):   
    def execute(self):
        TestBase.execute(self) # directly
        super(TestCase, self).execute() # or via super() proxy

由于您没有实际实例化
TestCase
,因此代码片段将无法工作。你到底想做什么?TestCase的一个对象是一个TestBase,但这种关系不是相互的。如果您的对象被实例化为TestBase,那么它仍然对其类的后代一无所知。TestBase不知道,也不应该知道关于TestCase的任何信息。如果它们在同一个文件中,那么这不应该起作用。检查堆栈帧怎么样?我想将测试用例功能抽象到“TestBase”。。。例如(打印、日志记录、状态更新,甚至方法的执行),如果TestBase没有定义execute属性,那么这不是递归的吗?当然不是,如果没有定义
TestBase.execute()
,就会抛出
AttributeError