[英]Do I have to define attributes in the base class in python?
這是一個“最佳實踐”問題,但是當我提交代碼時,我的IDE會向我發出警告,我不知道如何繼續。
類“BaseClass”的“未解析的屬性引用”屬性“。
基本上我有一個基類,有get和set方法。
class BaseClass(object):
def __set__(self, obj, value):
foo = obj.bar
foo.findmethod(self.attribute).setmethod(value)
然后,當我使用我將使用的實際類時,我從這個類繼承:
class ChildClass1(BaseClass):
attribute = this
class ChildClass2(BaseClass):
attribute = that
因為我永遠不會直接使用基類,所以我沒有理由在該級別定義此屬性。 但這個警告讓我想知道這是不好的做法。 如果我的命名慣例令人困惑,請道歉。
我同意@jonrsharpe,但不僅僅是在解決方法方面,而是作為一般做法。 我認為它非常適合自我文檔和代碼可讀性來定義您在初始化程序中或在實例生命中的其他地方,在類級別設置的屬性。 如果你記錄它們會更好。
class BaseClass(object):
attribute = None
'''Should be defined in a subclass. Use for something, here and there.'''
def __set__(self, obj, value):
foo = obj.bar
foo.findmethod(self.attribute).setmethod(value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.