>>> def checkIndex(key):
... if not isinstance(key,(int,long)):raise TypeError
... if key<0:raise IndexError
...
>>> class ArithneticSequence:
... def __init__(self,start=0,step=1):
... self.start=start
... self.step=step
... self.changed={}
... def __getitem__(self,key):
... checkIndex(key)
... try:return self.changed[key]
... except KeyError:
... return self.start+key*self.step
... def __setitem__(self,key,value):
... checkIndex(key)
... self.changed[key]=value
...
>>> s=ArithneticSequence(1,2)
>>> s[4]
9
>>> s[4]=2
>>> s[4]=2
>>> s[4]
2
>>> s[5]
11
>>> del s[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ArithneticSequence instance has no attribute '__delitem__'
>>> s["four"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __getitem__
File "<stdin>", line 2, in checkIndex
TypeError
>>> s[-42]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in __getitem__
File "<stdin>", line 3, in checkIndex
IndexError
相关文章
- Python:序列的copy() 方法和 copy 模块
- 3.Python爬虫入门三之Urllib和Urllib2库的基本使用
- 一个用于提取简体中文字符串中省,市和区并能够进行映射,检验和简单绘图的python模块
- RabbitMQ 优点和缺点- 消息可靠性:RabbitMQ 提供了持久化功能和消息确认机制,确保消息在各种情况下都能可靠地存储和处理。 灵活的路由:通过多种交换机类型和绑定规则,RabbitMQ 能够灵活地路由消息到指定的队列。 支持多种消息协议:实现了 AMQP 等(MQTT、STOMP)标准化、开放的消息队列协议,使其能够与多种语言编写的应用程序进行通信。 插件化扩展:RabbitMQ 提供了丰富的插件系统,可以通过插件扩展功能,如死信队列、压缩、追踪等。 高可用性:支持集群模式和镜像队列,确保服务的可用性 易用性和可管理性:提供了丰富的 API 和管理工具,以及多种客户端库和框架支持,易于集成和使用。 多语言支持:RabbitMQ 支持多种编程语言的客户端,包括 Java、Python、Ruby、C#、Node.js 等,方便开发人员集成到各种应用中。 高性能:在处理大量并发消息时表现出色。 广泛的社区支持:拥有庞大的开发者社区和丰富的文档资源。 劣势: 性能和吞吐量较低:相比于 Apache Kafka 等面向大数据流处理的消息队列系统,RabbitMQ 的吞吐量较低,不适合处理海量的实时数据流。RabbitMQ 的设计更注重消息的可靠性和灵活性,而非极高的吞吐性能。
- Python中Celery 的基本用法以及Django 结合 Celery 的使用和实时监控进程
- Python字典中的key和value取值的规则
- 传说中的华为Python笔试题——两等长整数序列互换元素,序列和的差值最小(修正)
- 『无为则无心』Python序列 — 19、Python列表的其他操作(切片和遍历)
- python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)
- 看Lucene源码必须知道的基本规则和算法