我有一个类A,它由一堆其他类继承.其中一些函数有一些相似的函数,将这些函数定义在其他地方并由需要它们的类调用会很好.但是这些函数调用超类中定义的函数.
class A():
def imp_func(*args):
# called by the child class functions
Class B(A):
def common_func(self):
# some stuff
self.imp_func(*args)
Run Code Online (Sandbox Code Playgroud)
所以我创建了我的辅助函数,它将self object参数作为参数,我可以imp_func在辅助函数中调用from.
def helper_func(obj, some_args):
# some common stuff
obj.imp_func(*args)
class B(A):
def common_func(self):
# unique stuff
helper_func(self, some_args)
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题.
但我应该这样做吗?这是Pythonic吗?
| 归档时间: |
|
| 查看次数: |
22871 次 |
| 最近记录: |