興味があるだけ、LEN()を使用し、デフ__len __(自己):クラスを構築する
は、私はクラスをビルドするときlen()
またはdef __len__()
を使用しての間に違い(長所と短所)はありますか?そして、これは最高のPythonスタイルですか?
class foo(object):
def __init__(self,obs=[])
self.data = obs
self.max = max(obs)
self.min = min(obs)
self.len = len(obs)
または
class foo(object):
def __init__(self,obs=[])
self.data = obs
self.max = max(obs)
self.min = min(obs)
def __len__(self):
return len(self.data)
第1のものと第2のものはどのように等価でしょうか? –
あなたが何をしているのか分からない限り、キーワードパラメータのデフォルト値として '[]'を使用しないでください。 [Python:変更可能なデフォルト引数]の「最小驚き」(http://stackoverflow.com/q/1132941)を参照してください。 –