@property
デコレータで設定された属性を持つクラスがあります。これらは、内部の try 句と except 句を使用してゲッターとセッターとして機能します。属性が設定されていない場合、データベースからデータを取得し、それを使用して他のクラスからオブジェクトをインスタンス化します。例を短くしようとしましたが、属性オブジェクトをインスタンス化するために使用されるコードは属性ごとに少し異なります。それらに共通しているのは、最初の try-except です。
class SubClass(TopClass):
@property
def thing(self):
try:
return self._thing
except AttributeError:
# We don't have any thing yet
pass
thing = get_some_thing_from_db('thing')
if not thing:
raise AttributeError()
self._thing = TheThing(thing)
return self._thing
@property
def another_thing(self):
try:
return self._another_thing
except AttributeError:
# We don't have things like this yet
pass
another_thing = get_some_thing_from_db('another')
if not another_thing:
raise AttributeError()
self._another_thing = AnotherThing(another_thing)
return self._another_thing
...etc...
@property
def one_more_thing(self):
try:
return self._one_more_thing
except AttributeError:
# We don't have this thing yet
pass
one_thing = get_some_thing_from_db('one')
if not one_thing:
raise AttributeError()
self._one_more_thing = OneThing(one_thing)
return self._one_more_thing
私の質問: これは適切な (たとえば pythonic) 方法ですか? すべての上に try-except-segment を追加するのは少し厄介に思えます。一方で、コードを短く保ちます。または、属性を定義するより良い方法はありますか?