我只写了几个星期的 Python 代码,所以我还在摸索这片土地的位置。但是,假设我有一个方法可以由“用户”偶尔调用,并且在内部大量使用(即,在调用之前已经检查了参数)。这是我目前正在做的事情:
#The method the 'user' should call:
def do_something(self, arg1, arg2, arg3):
#write code to do error checking on arg1, agr2, arg3
#raise exceptions, return codes, etc: depends on whether you are an explicit lover
#or an implicit lover, it seems. :-)
... error checking code here...
#Now call the 'brother' method that does the real work.
return self._do_something(self, arg1, arg2, arg3, arg3)
#The method other private methods should call with already validated parameters
def _do_something(self, arg1, arg2, arg3, arg3):
#don't do error checking on the parameters. get to work...
... do what you do...
return whatever you're supposed to return
这在我看来是合乎逻辑的。有没有更好的 Python 方式来做到这一点?
保罗