要么将_do_atomic_job()分解成一个适当的方法,要么分解它
进入自己的班级似乎是最好的办法。覆盖内部
函数无法工作,因为您无法访问
包含方法。在
你说_do_atomic_job()接受了很多参数,返回了很多值。也许您可以将这些参数中的一些组合成合理的对象:_do_atomic_job(start_x, start_y, end_x, end_y) # Separate coordinates
_do_atomic_job(start, end) # Better: start/end points
_do_atomic_job(rect) # Even better: rectangle
如果您不能这样做,并且_do_atomic_job()是相当自包含的,
您可以创建助手类AtomicJobParams和AtomicJobResult。
使用^{}代替类的示例:
^{pr2}$
最后,如果原子作业是自包含的,您甚至可以将它考虑到
自己的类AtomicJob。在class AtomicJob:
def __init__(self, a, b, c, d):
self.a = a
self.b = b
self.c = c
self.d = d
self._do_atomic_job()
def _do_atomic_job(self):
...
self.result_1 = 42
self.result_2 = 23
self.result_3 = 443
总的来说,这更像是一个代码分解问题。力求精瘦
在适当的情况下将工作委托给助手的类。跟随single responsibility principle。如果值属于一起,则将它们打包在一个值类中。在
作为David Miller(著名的Linux内核开发人员)recently said:If you write interfaces with more than 4 or 5 function arguments, it's
possible that you and I cannot be friends.