私のクラスの1つは、オブジェクトのコレクションに対して多くの集計計算を実行してから、特定のオブジェクトに適切な属性と値を割り当てます。
class Team(object):
def __init__(self, name): # updated for typo in code, added self
self.name = name
class LeagueDetails(object):
def __init__(self): # added for clarity, corrected another typo
self.team_list = [Team('name'), ...]
self.calculate_league_standings() # added for clarity
def calculate_league_standings(self):
# calculate standings as a team_place_dict
for team in self.team_list:
team.place = team_place_dict[team.name] # a new team attribute
が実行されている限りcalculate_league_standings
、すべてのチームが実行されていることを私は知っていますteam.place
。私ができるようにしたいのは、コードをスキャンしてclass Team(object)
、クラスメソッドによって作成された属性と、クラスオブジェクトを操作する外部メソッドによって作成されたすべての属性を読み取ることです。for p in dir(team): print p
属性名が何であるかを確認するためだけに入力するのに少しうんざりしています。attributes
チームで空白の束を定義できます__init__
。例えば
class Team(object):
def __init__(self, name): # updated for typo in code, added self
self.name = name
self.place = None # dummy attribute, but recognizable when the code is scanned
calculate_league_standings
戻っteam._place
てから追加するのは冗長なようです
@property
def place(self): return self._place
上部にある属性のリストにコメントできることはわかっていますがclass Team
、これは明らかな解決策ですが、ここにはベストプラクティスが必要であり、ここではPythonのようなエレガントなものが必要だと感じています。