我有一个方法,它根据标志调用其他一些方法..
def methodA(self):
if doc['flag']:
self.method1()
self.method2()
现在,我必须从另一个地方调用 methodA() ,它需要做基本上相同的事情,但独立于 doc['flag'] (调用 self.method1() 和 self.method2()无论标志是真还是假)有没有一个好的方法来做到这一点?
谢谢
最佳答案
一种方法是:
def methodA(self):
if doc['flag']:
self.anotherMethod()
def anotherMethod(self):
self.method1()
self.method2()
或者:
def methodB(self, flag=False, execute_anyways=False):
if not flag and not execute_anyways:
return #Note that while calling, you would be sending True, or False. If flag=None, it would execute.
self.method1()
self.method2()
def methodA(self):
self.methodB(flag=doc['flag'])
在另一种情况下,只需调用
self.methodB(execute_anyways=True) #Now, the flag would have the default value of None, and would execute.
关于python - 重用执行标志检查的方法中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395521/