python写方法函数_重写python中方法的内部函数

要么将_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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值