Pythonでダックタイピングを使用しています。
def flagItem(object_to_flag, account_flagging, flag_type, is_flagged):
if flag_type == Flags.OFFENSIVE:
object_to_flag.is_offensive=is_flagged
elif flag_type == Flags.SPAM:
object_to_flag.is_spam=is_flagged
object_to_flag.is_active=(not is_flagged)
object_to_flag.cleanup()
return object_to_flag.put()
さまざまなオブジェクトがとして渡されるobject_to_flag
場合、そのすべてに、、is_active
属性があります。彼らはまた、方法を持っています。is_spam
is_offensive
cleanup()
渡すオブジェクトはすべて同じ基本クラスを持っています(Google App Engineのdbオブジェクトです)。
class User(db.Model):
...
is_active = db.BooleanProperty(default = True)
is_spam = db.BooleanProperty(default=False)
is_offensive = db.BooleanProperty(default=False)
def cleanup():
pass
class Post(db.Model):
...
is_active = db.BooleanProperty(default = True)
is_spam = db.BooleanProperty(default=False)
is_offensive = db.BooleanProperty(default=False)
def cleanup():
pass
cleanup()
メソッドを抽象化して、子が実装を提供する必要があるこれらすべてのオブジェクトに対して同じ親クラスを持つことができるよう にするにはどうすればよいですか?
おそらくもっと重要なのは、これは「pythonic」ですか?このルートに行くべきですか、それともダックタイピングだけに頼るべきですか?私のバックグラウンドはJavaで、Pythonのやり方を学ぼうとしています。
ありがとう!