1

フラグに基づいて他のいくつかのメソッドを呼び出すメソッドがあります..

def methodA(self):
    if doc['flag']:
         self.method1()      
         self.method2()

ここで、本質的に同じことを行う必要がある別の場所から methodA() を呼び出す必要がありますが、doc['flag'] とは無関係です (self.method1() および self.method2() を呼び出すかどうかに関係なく)。 flag は true または false) これを行う良い方法はありますか?

ありがとう

4

1 に答える 1

2

それを行う1つの方法は次のとおりです。

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. 
于 2013-06-30T22:50:19.890 に答える