>>> 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 基本的序列和映射规则的更多相关文章

  1. python魔法方法、构造函数、序列与映射、迭代器、生成器

    在Python中,所有以__双下划线包起来的方法,都统称为"魔术方法".比如我们接触最多的__init__,魔法方法也就是具有特殊功能的方法. 构造函数 构造函数不同于普通方法,将 ...

  2. Python 序列与映射的解包操作

    解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...

  3. Python 序列与映射的解包操作-乾颐堂

    解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...

  4. Python数据结构:序列(列表[]、元组())与映射(字典{})语法总结

    一.概述:Python中两种基本的数据结构是序列和映射,序列包含:可变的列表和不可变的元组:而当序列不够用时就出现了映射:字典.列表中的元素是可以变化的,元组里面的元素一旦初始化后就不可更改.列表和元 ...

  5. python——序列 & 集合 & 映射

    列表 & 元组 & 字典 & 集合 序列 序列: 每个元素可以是任何类型(也可以是序列),每个元素被分配一个序号(从0开始)(序号,也叫索引,表示元素的位置) Python中的 ...

  6. Python_序列与映射的解包操作

    解包就是把序列或映射中每个元素单独提取出来,序列解包的一种简单用法就是把首个或前几个元素与后面几个元素分别提取出来,例如: first, seconde, *rest = sequence 如果seq ...

  7. SpringMVC注解汇总(二)-请求映射规则

    接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...

  8. Python数据类型之“序列概述与基本序列类型(Basic Sequences)”

    序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要以下几种类型: 3种基本序列类型(Basic Sequence Types):list. ...

  9. 详解 Python 中的下划线命名规则

    在 python 中,下划线命名规则往往令初学者相当 疑惑:单下划线.双下划线.双下划线还分前后……那它们的作用与使用场景 到底有何区别呢?今天 就来聊聊这个话题. 1.单下划线(_) 通常情况下,单 ...

随机推荐

  1. ubuntu 14.04 cagl

    libboost-atomic1.-dev libboost-atomic1.-dev libboost-chrono1.-dev libboost-dev libboost-program-opti ...

  2. Jbpm工作流表补数记录

    一: 历史数据表 11.  JBPM4_HIST_ACTINST 流程活动(节点)实例表 存放Activity Instance的历史记录 12.  JBPM4_HIST_DETAIL  流程历史详细 ...

  3. ESFramework 4.0 进阶(04)-- 驱动力:通信引擎(下)

    在ESFramework 4.0 进阶(03)-- 驱动力:通信引擎(上)一文中,我们对ESFramework提供的每一个通信引擎的接口都做了详细了说明,这篇文章我们将继续探讨这些接口的实现类 -- ...

  4. .Net Core 学习资料

    官方网站:https://www.microsoft.com/net/core#windows   官方文档:https://docs.asp.net/en/latest/intro.html   中 ...

  5. Java中需要总结的几个问题

    慢慢总结,不然每次百度挺心烦的. 1. java文件的读写 2. String和StringBuffer的区别

  6. Chapter 15_1 require函数

    Lua提供了一个名为require的高层函数来加载模块,但这个函数只假设了关于模块的基本概念. 对于require而言,一个模块就是一段定义了一些值(函数或者包含函数的table)的代码. 为了加载一 ...

  7. UISlider 滑竿控件

    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake:(100, 100, 200, 25);  //创建一个滑竿对象 slide ...

  8. ios 将图片变成圆形

    #pragma mark - 将图片转换成圆形 -(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset { UIGraph ...

  9. Ubuntu14.04下安装redis

    1.首先在官网上下载redis压缩包 redis-3.2.0.tar.gz 2.解压到到当前文件夹(这里可以解压到随意位置) tar zvxf redis-3.2.0.tar.gz 3.切换到redi ...

  10. 【python问题系列--2】脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level

    缩进错误,此错误,最常见的原因是行之间没有对齐. 参考:http://www.crifan.com/python_syntax_error_indentationerror/comment-page- ...