クラスAのオブジェクトは、ネットワーク接続に似ています。つまり、引数としてハンドル(特定の接続)を持つ異なるメソッドを呼び出します。クラスのメソッド引数をカプセル化するPythonの方法
がclass A(object):
def __init__(self, *args):
... some init
def my_open(self, *args)
handle = ... some open
return handle
def do_this(self, handle, *args):
foo_this(handle, args)
def do_that(self, handle, *args):
foo_that(handle, args)
典型的な使用法は、特定の状況で、今
a = A(args)
handle = a.my_open(args2)
a.do_this(handle, args3)
あるの世話をするだけで一つの接続、劇中すなわち、1つのハンドルがあり、:のように私のクラスA(のpython 2.7)が見えます。だから、このハンドルを隠すことは妥当ですが、より一般的な状況のためにクラスAを保つことは妥当です。このように、クラスAの一種「である」クラスB 上の私の最初の考えは(使用量は同じままが、ハンドルが非表示になります)、次のとおりです。
class B(A):
def __init__(self, *args):
super(A, self).__init__(args)
self.handle = None
def my_open(self, *args):
self.handle = super(A, self).__init__(args)
def do_this(self, *args):
super(A, self).do_this(self.handle, args)
def do_that(self, *args):
super(A, self).do_that(self.handle, args)
残念ながら、私の意見では、それは非常に複雑なようです。どんな良いアイデアですか?
一貫性を保つために、私はそのハンドル**を返して、それをシングルトンにします。 –
そして実際には、 'do_this()'と 'do_that()' *メソッドをハンドルオブジェクト*に配置する必要があります。つまり、 'handle = a.my_open(args2)'、 'handle.do_this(args3)'です。 –
1つのAオブジェクトでハンドルを開き、別のオブジェクトでハンドルを使用するか、最初のAを別のハンドルで使用できるように、システムが構築されています。私にとっては混乱です。もっと整理してください。 – Zefick