Python 定义只读属性的实现方式
Python是面向对象(OOP)的语言, 而且在OOP这条路上比Java走得更彻底, 因为在Python里, 一切皆对象, 包括int, float等基本数据类型.
在Java里, 若要为一个类定义只读的属性, 只需要将目标属性用private修饰, 然后只提供getter()而不提供setter(). 但Python没有private关键字, 如何定义只读属性呢? 有两种方法, 第一种跟Java类似, 通过定义私有属性实现. 第二种是通过__setattr__.
通过私有属性
Python里定义私有属性的方法见 https://www.jb51.net/article/181953.htm.
用私有属性+@property定义只读属性, 需要预先定义好属性名, 然后实现对应的getter方法.
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
所以呢, 只需要在__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 ONLY
以上这篇Python 定义只读属性的实现方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
tensorflow从ckpt和从.pb文件读取变量的值方式
这篇文章主要介绍了tensorflow从ckpt和从.pb文件读取变量的值方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-05-05
Python常见库matplotlib学习笔记之画图文字的中文显示
在Python中使用matplotlib或者plotnine模块绘图时,常常出现图表中无法正常显示中文的问题,下面这篇文章主要给大家介绍了关于Python常见库matplotlib学习笔记之画图文字的中文显示的相关资料,需要的朋友可以参考下2023-05-05
Visual Studio Code搭建django项目的方法步骤
这篇文章主要介绍了Visual Studio Code搭建django项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09


最新评论