私はABCをPythonで使用して、特定のインターフェイスへのコーディングを強制しました。しかし、メソッドが実装されていないクラスを作成し、実際の実装で継承してオーバーライドするだけで、基本的に同じことができるようです。 ABCがなぜ追加されたのか、なぜこのコーディングパターンを言語でより顕著にするために、もっとPythonの理由がありますか?以下はなぜNotImplementedメソッドを持つ通常のクラスの代わりに抽象基本クラスを使用しますか?
は、私はそれが抽象的な「オプティマイザ」のインターフェイスを定義するために、私の「NotImplemented」スキームを実装して書いたコードスニペットです:
class AbstractOptimizer(object):
'''
Abstract class for building specialized optimizer objects for each use case.
Optimizer object will need to internally track previous results and other data so that it can determine the
truth value of stopConditionMet().
The constructor will require a reference argument to the useCaseObject, which will allow the optimizer
to set itself up internally using fields from the use case as needed. There will need to be a close coupling between
the optimizer code and the use case code, so it is recommended to place the optimizer class definition in the same module
as the use case class definition.
Interface includes a constructor and four public methods with no arguments:
0) AbstractOptimizer(useCaseObject) returns an instance of the optimizer
1) getNextPoint() returns the current point to be evaluated
2) evaluatePoint(point) returns the modeling outputs "modelOutputs" evaluated at "point"
3) scorePoint(evaluationResults,metric) returns a scalar "score" for the results output by evaluatePoint according to metric. NOTE: Optimization is set up as a MINIMIZATION problem, so adjust metrics accordingly.
4) stopConditionMet(score) returns True or False based on whatever past result are needed for this decision and the current score. If score = None, it is asumed to be the start of the optimization.
5) getCurrentOptimalPoint() returns currently optimal point along with its iterationID and score
The calling workflow will typically be: getNextPoint() -> evaluatePoint() -> scorePoint -> stopConiditionMet -> repeat or stop
'''
def __init__(self, useCaseObject):
'''
useCaseObject is a reference to use case instance associated with the optimizer.
This optimizer will be "coupled" to this use case object.
'''
return NotImplemented
def stopConditionMet(self,score = None):
'''
Returns True or False based on whether the optimization should continue.
'''
return NotImplemented
def getNextPoint(self):
'''
If stopConditionMet() evaluates to False, then getNextPoint() should return a point; otherwise, it should return None
'''
return NotImplemented
def evaluatePoint(self,point):
'''
Returns the modeling outputs "modelOutputs" evaluated at the current point.
Will utilize the linked
'''
return NotImplemented
def scorePoint(self,evaluationResults,metric):
'''
Returns a scalar "score" for the current results evaluated in at the current point (from evaluatePoint) based on the function "metric"
Note that "metric" will likely need to be a closure with the evaluation results as bound objects.
'''
return NotImplemented
def getCurrentOptimum(self):
'''
returns currently optimal point and it's score as a dictionary: optimalPoint = {"point":point,"score":score}
'''
return NotImplemented
[NotImplementedError]、['NotImplemented'はPythonの比較メカニズムに関連する定数値です](https://docs.python.org/2/library/constants.html#NotImplemented)を呼び出す必要があります。それは意味論的意味が異なっている。 –
@ŁukaszRogalskiありがとう!良いキャッチ...それはオーバーライドを強制します。上記をNotImplementedErrorsに変更すると、ABCについて何が欠けていますか?特にPython 2と3はこれを別の方法で扱いますから? –
[ABCMetaから派生したメタクラスを持つクラスは、抽象メソッドとプロパティのすべてがオーバーライドされていない限りインスタンス化できません._ '__init__'だけをオーバーライドすれば、サブクラスがインスタンス化するのに十分です。 –