我想为一个类实现一个方法-这个方法将使用类中其他方法的结果,但是它将有100行长,所以如果可能的话,我想在另一个文件中定义这个方法。我该怎么做?像这样:
ParentModule.py
:
import function_defined_in_another_file
def method(self):
return function_defined_in_another_file.function()
ParentModule是我不想在其中定义函数的主模块。你知道吗
function_defined_in_another_file.py
:
import ParentModule
def function():
a = ParentModule.some_method()
b = ParentModule.some_other_method()
return a + b
在另一个文件中定义的函数必须能够使用ParentModule中提供的方法。你知道吗
我这样做是否合适,还是有更好的方法?你知道吗
您可以将方法分配给类:
它的处理方式与任何其他方法一样;您可以对
SomeClass()
的实例调用method
,其他SomeClass()
方法可以用self.method()
调用它,method()
可以用self.method_name()
调用任何SomeClass()
方法。你知道吗必须确保
function()
接受self
参数。你知道吗相关问题 更多 >
编程相关推荐