我一直在寫Python代碼只有幾個星期,所以我仍然在計算出這片土地。但是讓我們假設我有一個方法可以被一個'用戶'偶爾以及內部使用的HEAVILY調用(即參數在調用之前已經被檢查過)。這是我目前正在做的: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-ISH方式來做到這一點?
保羅
是的,謝謝你。 – TallPaul 2009-10-27 17:03:33
按建議編輯方法調用。 – TallPaul 2009-10-27 17:14:37