zoukankan      html  css  js  c++  java
  • python 基本的序列和映射规则

    >>> 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

  • 相关阅读:
    汇编写启动代码之关看门狗、设置栈、调用C、开关icache
    ARM汇编伪指令
    多寄存器访问、后缀、栈、!、^
    协处理器CP15操作指令
    常用的ARM指令
    汇编指令及其特点
    ARM的37个寄存器以及异常处理方法
    一步步点亮LED之汇编点亮LED
    机器学习_第一节_numpy
    函数进阶_生成器
  • 原文地址:https://www.cnblogs.com/yhcreak/p/5363338.html
Copyright © 2011-2022 走看看