Python的简单不可变类型。
项目描述
itypes
Python的基本不可变容器类型。
一个以简单为设计目标,而牺牲性能的简单实现。
在可能产生更易于理解的代码的情况下使用,或者当您想创建具有受限、不可变接口的自定义类型时。
有关针对性能设计的替代实现,请参阅 pyrsistent。
安装
使用pip
安装
pip install itypes
实例化字典和列表。
>>> import itypes
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> l = itypes.List(['a', 'b', 'c'])
在实例化时,嵌套类型被强制转换为不可变类型。
>>> d = itypes.Dict({'a': 123, 'b': ['a', 'b', 'c']})
>>> d['b']
List(['a', 'b', 'c'])
赋值和删除返回新的副本。
方法: set(key, value)
,delete(key)
>>> d2 = d.set('c', 456)
>>> d2
Dict({'a': 123, 'b': ['a', 'b', 'c'], 'c': 456})
>>> d3 = d2.delete('a')
>>> d3
Dict({'b': ['a', 'b', 'c'], 'c': 456})
标准赋值和删除失败。
>>> d['z'] = 123
TypeError: 'Dict' object doesn't support item assignment
>>> del(d['c'])
TypeError: 'Dict' object doesn't support item deletion
嵌套查找。
方法: get_in(keys, default=None)
>>> d['b'][-1]
'c'
>>> d['b'][5]
IndexError: list index out of range
>>> d.get_in(['b', -1])
'c'
>>> d.get_in(['b', 5])
None
嵌套赋值和删除。
方法: set_in(keys, value)
,delete_in(keys)
>>> d2 = d.set_in(['b', 1], 'xxx')
>>> d2
Dict({'a': 123, 'b': ['a', 'xxx', 'c']})
>>> d3 = d2.delete_in(['b', 0])
>>> d3
Dict({'a': 123, 'b': ['xxx', 'c']})
与标准类型相等。
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> d == {'a': 1, 'b': 2, 'c': 3}
True
对象可哈希。
>>> hash(d)
277752239
在可变类型和不可变类型之间切换的快捷方式。
功能: to_mutable(instance)
、to_immutable(value)
>>> value = itypes.to_mutable(d)
>>> value
{'a': 123, 'b': ['a', 'b', 'c']}
>>> itypes.to_immutable(value)
Dict({'a': 123, 'b': ['a', 'b', 'c']})
子类化。
只能对实例设置私有属性名称。使用 @property
进行属性访问。
如果对象有额外的状态,请定义一个 .clone(self, data)
方法。
示例
class Configuration(itypes.Dict):
def __init__(self, title, *args, **kwargs):
self._title = title
super(Configuration, self).__init__(*args, **kwargs)
@property
def title(self):
return self._title
def clone(self, data):
return Configuration(self._title, data)
使用自定义类
>>> config = Configuration('worker-process', {'hostname': 'example.com', 'dynos': 4})
>>> config.title
'worker-process'
>>> new = config.set('dynos', 2)
>>> new
Configuration({'dynos': 2, 'hostname': 'example.com'})
>>> new.title
'worker-process'
自定义不可变对象。
为了防止设置公共属性,子类化 itypes.Object
。
>>> class Custom(itypes.Object):
... pass
只能对实例设置私有属性名称。使用 @property
进行属性访问。
>>> class Document(itypes.Object):
... def __init__(self, title, content):
... self._title = title
... self._content = title
... @property
... def title(self):
... return self._title
... @property
... def content(self):
... return self._content
使用不可变对象
>>> doc = Document(title='Immutability', content='For simplicity')
>>> doc.title
'Immutability'
>>> doc.title = 'Changed'
TypeError: 'Document' object doesn't support property assignment.
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源分布
itypes-1.2.0.tar.gz (4.4 kB 查看哈希值)
构建分布
itypes-1.2.0-py2.py3-none-any.whl (4.8 kB 查看哈希值)
关闭
itypes-1.2.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | af886f129dea4a2a1e3d36595a2d139589e4dd287f5cab0b40e799ee81570ff1 |
|
MD5 | 8c8e98875d885e87074d071c447cd9c4 |
|
BLAKE2b-256 | 0e53764524b3907d0af00523f8794daca181c08ca7cb32ceee25a0754d5e63a5 |
关闭
itypes-1.2.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 03da6872ca89d29aef62773672b2d408f490f80db48b23079a4b194c86dd04c6 |
|
MD5 | c5fe660c8dda870f7e64896c8f324bc4 |
|
BLAKE2b-256 | 3fbb3bd99c7cd34d4a123b2903e16da364f6d2078b1c3a3530a8ad105c668104 |