Python 如何定義只讀屬性?【新手必學】


 

在Java里, 若要為一個類定義只讀的屬性, 只需要將目標屬性用private修飾, 然后只提供getter()而不提供setter(). 但Python沒有private關鍵字, 如何定義只讀屬性呢? 有兩種方法, 第一種跟Java類似, 通過定義私有屬性實現. 第二種是通過__setattr__.

通過私有屬性

用私有屬性+@property定義只讀屬性, 需要預先定義好屬性名, 然后實現對應的getter方法.,如果對屬性還不懂。那建議你先去小編的Python學習.裙 :一久武其而而流一思(數字的諧音)轉換下可以找到了,里面有最新Python教程項目,先系統學習下!

class Vector2D(object): def __init__(self, x, y): self.__x = float(x) self.__y = float(y) @property def x(self): return self.__x @property def y(self): return self.__y if __name__ == "__main__": v = Vector2D(3, 4) print(v.x, v.y) v.x = 8 # error will be raised.

輸出:

(3.0, 4.0) Traceback (most recent call last): File ...., line 16, in <module> v.x = 8 # error will be raised. AttributeError: can't set attribute

可以看出, 屬性x是可讀但不可寫的.

通過__setattr__

當我們調用obj.attr=value時發生了什么?

很簡單, 調用了obj__setattr__方法. 可通過以下代碼驗證:

class MyCls(): def __init__(self): pass def __setattr__(self, f, v): print 'setting %r = %r'%(f, v) if __name__ == '__main__': obj = MyCls() obj.new_field = 1

輸出:

setting 'new_field' = 1
  • 1

所以呢, 只需要在__setattr__ 方法里擋一下, 就可以阻止屬性值的設置, 可謂是釜底抽薪. 
代碼:

# encoding=utf8 class MyCls(object): readonly_property = 'readonly_property' def __init__(self): pass def __setattr__(self, f, v): if f == 'readonly_property': raise AttributeError('{}.{} is READ ONLY'.\ format(type(self).__name__, f)) else: self.__dict__[f] = v if __name__ == '__main__': obj = MyCls() obj.any_other_property = 'any_other_property' print(obj.any_other_property) print(obj.readonly_property) obj.readonly_property = 1

輸出:

any_other_property
readonly_property
Traceback (most recent call last):
  File "...", line 21, in <module> obj.readonly_property = 1 ... AttributeError: MyCls.readonly_property is READ ONL


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM