0

所以我使用的模块是这样的:

#module test.A
from test.stuff import DoStuff

class A(object):

  def runs(self):
    #......
    #...lots of code
    DoStuff.objects.something()
    #...lots of code

我需要用其他东西替换 DoStuff,所以我所做的是我使用 A 类的地方:

#my module
import test.stuff
from my.stuff import MyDoStuff
test.stuff.DoStuff = MyDoStuff

from test.A import A

class B(A):
  #define other things but not runs
  #since I would have to copy a lot of 
  #code over

最后我在另一个模块的另一个函数中使用这个类 B。这似乎有效。我一直在这里阅读其他问题,并且有很多建议不要做猴子补丁——所以在这种情况下,我想知道为什么/为什么不是一个好主意,还有什么是更好的方法来完成一样?

谢谢你。

4

0 回答 0