创建无法更改的类属性。
项目描述
Python只读属性
=========================
背景
-----------
> 通常,Python程序应假定
> 所有用户都是同意成年人,因此应自行
> 负责任地正确使用。
[Silas Ray][1] 在Stackoverflow。因此,一般来说,Python不隐藏
太多。Python中没有“私有”与“公共”的概念,因此
一切都是可写的。
但如果你或你的代码用户忘记了怎么办?
如果你真的不能假设每个人都怎么样?
我们是否放弃在Python中拥有只读属性?
简短介绍
-------------------
此模块允许您使类属性只读,而无需
使用`@property`或`__getattr__`。
为什么不使用`@property`?
因为它很冗长,并且您需要创建一个返回
某个东西的函数。
```
class MyClass
@property
def a(self)
return 1
```
上面的代码可以简化为
```
@read_only_properties('a')
class MyClass(object)
def __init__(self, a, b, c)
self.readonly = a
```
在构造函数中分配后,您无法更改它。
用法
------
此软件包安装了一个名为`rop.py`的单个Python模块,其中包含一个名为`read_only_properties`的单个
装饰器。要使用它,只需导入装饰器并
装饰您的类。
```
from rop import read_only_properties
@read_only_properties('b')
class Foo
def __init__(self, a, b)
self.a
self.b
```
从@property迁移
----------------------
如果你有一个具有许多属性并且想要
重构为属性的类
```
class AClassWithManyAttributes
def __init__(a, b, c, d, e ...)
self.a = a
self.b = b
self.c = c
``` ....
上面的类将非常冗长(IDE将节省你很多
输入,但不会缩短代码
```
class AClassWithManyAttributes
'''重构为属性'''
def __init__(a, b, c, d, e ...)
self._a = a
self._b = b
self._c = c
@property
def a(self)
return self._a
@property
def b(self)
return self._b
@property
def b(self)
return self._c
# 你会得到这个...它很长
```
现在想象你可以这样做
```
@read_only_properties('a', 'b', 'c')
class AClassWithManyAttributes
def __init__(a, b, c, d, e)
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
```
这使得属性`a, b, c`为只读,尝试重新分配任何其中一个的值
将引发异常,其他类属性不受影响。
此模块的代码最初来源于
[作者的Stackoverflow答案][2]。
[1]: http://stackoverflow.com/a/14594174/492620
[2]: http://stackoverflow.com/a/35906068/492620
=========================
背景
-----------
> 通常,Python程序应假定
> 所有用户都是同意成年人,因此应自行
> 负责任地正确使用。
[Silas Ray][1] 在Stackoverflow。因此,一般来说,Python不隐藏
太多。Python中没有“私有”与“公共”的概念,因此
一切都是可写的。
但如果你或你的代码用户忘记了怎么办?
如果你真的不能假设每个人都怎么样?
我们是否放弃在Python中拥有只读属性?
简短介绍
-------------------
此模块允许您使类属性只读,而无需
使用`@property`或`__getattr__`。
为什么不使用`@property`?
因为它很冗长,并且您需要创建一个返回
某个东西的函数。
```
class MyClass
@property
def a(self)
return 1
```
上面的代码可以简化为
```
@read_only_properties('a')
class MyClass(object)
def __init__(self, a, b, c)
self.readonly = a
```
在构造函数中分配后,您无法更改它。
用法
------
此软件包安装了一个名为`rop.py`的单个Python模块,其中包含一个名为`read_only_properties`的单个
装饰器。要使用它,只需导入装饰器并
装饰您的类。
```
from rop import read_only_properties
@read_only_properties('b')
class Foo
def __init__(self, a, b)
self.a
self.b
```
从@property迁移
----------------------
如果你有一个具有许多属性并且想要
重构为属性的类
```
class AClassWithManyAttributes
def __init__(a, b, c, d, e ...)
self.a = a
self.b = b
self.c = c
``` ....
上面的类将非常冗长(IDE将节省你很多
输入,但不会缩短代码
```
class AClassWithManyAttributes
'''重构为属性'''
def __init__(a, b, c, d, e ...)
self._a = a
self._b = b
self._c = c
@property
def a(self)
return self._a
@property
def b(self)
return self._b
@property
def b(self)
return self._c
# 你会得到这个...它很长
```
现在想象你可以这样做
```
@read_only_properties('a', 'b', 'c')
class AClassWithManyAttributes
def __init__(a, b, c, d, e)
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
```
这使得属性`a, b, c`为只读,尝试重新分配任何其中一个的值
将引发异常,其他类属性不受影响。
此模块的代码最初来源于
[作者的Stackoverflow答案][2]。
[1]: http://stackoverflow.com/a/14594174/492620
[2]: http://stackoverflow.com/a/35906068/492620
项目详情
关闭
read-only-property-0.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 347f28042fe95cd78ab57ab5d6ab50fc74e9757b1530e8b129b046990250183d |
|
MD5 | 4a5700c0f479383036557e87b169225d |
|
BLAKE2b-256 | 930ed202a0cb5ef4fce40e5285d7094a1b6d1bc33392d1116e938e834645d7fd |