前面在介绍了类的很多内置方法,比如__add__,__eq__,这里继续介绍类的两个内置方法,这2个内置方法可以将一个类实例变成一个序列的形式。代码如下
class vector(object):
    def __init__(self,components):
        self._components=components
        print self._components
    def __len__(self):
        return len(self._components)
    def __getitem__(self,position):
        return self._components[position] if __name__=="__main__":
    v=vector([1,2,3])
    print len(v)
    print v[:3]
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
[1, 2, 3]
3
[1, 2, 3]
首先初始化的时候传入一个列表并赋值给_components。然后再调用len(v)的时候会自动调用__len__,返回_components的长度。调用v[1]或者v[0:3]的时候会调用__getitem__的方法,返回对应的_components的数据。
这里比较有意思的是,我们在__getitem__中我们返回的是self._components[position],这是代表position这个位置的元素。但是如果传递的是v[:3]的方式,也就是切片的方式,依然能够返回切片的结果。我们把代码修改下:
class vector(object):
    def __init__(self,components):
        self._components=components
        print self._components
    def __len__(self):
        return len(self._components)
    def __getitem__(self,position):
        print type(position)
        return position if __name__=="__main__":
    v=vector([1,2,3])
    print len(v)
    print v[:3]
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
[1, 2, 3]
3
<type 'slice'>
slice(None, 3, None)
可以看到position的类型是slice,而且返回的是slice(None,3,None).那么slice是什么类型呢
Slice其实是个切片的对象。我们来看下用法
s=slice(0,2,1)
print a[s]
运行结果
[1, 2]
在这里slice(0,2,1)的原型是slice[start,stop,step].也就是说slice[0,2,1]其实是等于a[0:2]。看到这里我们就明白了。由于position是一个slice对象,且这个slice对象为slice(None, 3, None)因此结果也就等于self. _components[:3]
 
__getattr__和__setattr__方法:
首先来看下vector代码:
class vector(object):
    shortcut_names="xyzt"
    def
__init__(self,components):
        self._components=components
    def __len__(self):
        return len(self._components)
    def __getitem__(self,position):
        print type(position)
        return position
    def __str__(self):
        return "vector"
    def
__getattr__(self, name):
        cls=type(self)
        print cls
        if len(name) == 1:
            pos=cls.shortcut_names.find(name)
            if 0<=pos<=len(self._components):
                return self._components[pos] if __name__=="__main__":
    v=vector(range(1,10))
    print "before add attribute %s" % v.__dict__   ⑴
    print v.x           ⑵
    v.x=10              ⑶
    print v.x
    print "after add attribute %s" % v.__dict__    :⑷
 
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
before add attribute {'_components': [1, 2, 3, 4, 5, 6, 7, 8, 9]}
<class '__main__.vector'> 
1 
10
after add attribute {'_components': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'x': 10}
 
首先在第一步的时候,v的属性只有一个列表。在第二步的时候print v.x。此时会检测v实例中是否有x属性,如果没有则会到类v.__class__中去寻找,如果仍然没找到,则会调用__getattr__方法。在__getattr__方法中,首先找到x在shortcut_names中的索引位置,然后根据这个索引找到_components中对应的数字。
在第三步的时候,我们设置了v.x=10.然后再打印v.x的时候,此时由于v.x=10导致v实例添加了x属性,因此不会调用__getattr__方法。查看v.__dict__也可以看到属性中新增了x属性。但这会导致一个问题,我们的目的是想通过x的索引位置得出_components对应位置的值,但是由于中途改变了x的值,导致我们在继续调用v.x的时候达不到我们的目的。因此有必要将这些在shortcut_names出现的值设置为可读。代码修改如下:
class vector(object):
    shortcut_names="xyzt"
    def
__init__(self,components):
        self._components=components
        print self._components
    def __len__(self):
        return len(self._components)
    def __str__(self):
        return "vector"
    def
__getattr__(self, name):
        cls=type(self)
        if len(name) == 1:
            pos=cls.shortcut_names.find(name)
            if 0<=pos<=len(self._components):
                return self._components[pos]
    def __setattr__(self, name, value):
        cls=type(self)
        if len(name) == 1:
            if name in cls.shortcut_names:
                error="readonly attributes {attr_name!r}"
            elif
name.islower():
                error="can't set attributes 'a' to 'z' in {cls_name!r}"
            else
:
                error=''
            if
error:
                msg=error.format(cls_name=cls.__name__,attr_name=name)
                raise AttributeError(msg)
        super(vector, self).__setattr__(name,value) if __name__=="__main__":
    v=vector(range(1,10))
    print v.x
    v.x=10
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Traceback (most recent call last):
1
  File "E:/py_prj/fluent_python/chapter10.py", line 39, in <module>
    v.x=10
  File "E:/py_prj/fluent_python/chapter10.py", line 31, in __setattr__
    raise AttributeError(msg)
AttributeError: readonly attributes 'x'
新增__setattr__方法,当对属性进行赋值的时候,首先会调用__setattr__方法。在方法中对属性进行判断和保护。从执行结果来看,当执行v.x=10的时候。产生错误AttributeError: readonly attributes 'x'。这样就可以避免对x进行赋值了 
 
最后来看一个__hash__的用法。还是以之前vector的例子,我们想要要用异或运算符依次计算各个分量的散列值。比如v[0]^v[1]^v[2].来看下代码实现:
class vector(object):
    shortcut_names="xyzt"
    def
__init__(self,components):
        self._components=components
    def __len__(self):
        return len(self._components)
    def __str__(self):
        return "vector"
    def
__getattr__(self, name):
        cls=type(self)
        if len(name) == 1:
            pos=cls.shortcut_names.find(name)
            if 0<=pos<=len(self._components):
                return self._components[pos]
    def __setattr__(self, name, value):
        cls=type(self)
        if len(name) == 1:
            if name in cls.shortcut_names:
                error="readonly attributes {attr_name!r}"
            elif
name.islower():
                error="can't set attributes 'a' to 'z' in {cls_name!r}"
            else
:
                error=''
            if
error:
                msg=error.format(cls_name=cls.__name__,attr_name=name)
                raise AttributeError(msg)
        super(vector, self).__setattr__(name,value)
    def __hash__(self):
        hashes=(hash(x) for x in self._components)
        print hashes
        return reduce(operator.add,hashes) if __name__=="__main__":
    v=vector(range(1,10))
    print hash(v)
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
<generator object <genexpr> at 0x017B8148>
1
新增了__hash__方法。首先hashes得到各个向量值的hash值。然后通过reduce运算得到最终的异或值
这里简单介绍下reduce函数:
def add(x,y):
    return x+y
a=[1,2,3,4,5]
print reduce(add,a)
E:\python2.7.11\python.exe E:/py_prj/fluent_python/chapter10.py
15
Reduce函数有2个参数值,第一个是执行函数,第二个是作用对象。例如reduce(fn,lst),fn首先会应用到第一对元素上,fn(lst[0],lst[1])生成一个结果r 然后fn会应用到r和下一个元素上,fn(r,lst[2]).依次按照这个方式进行调用,直到最后一个元素

流畅python学习笔记:第十章:序列的修改,散列和切片的更多相关文章

  1. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  2. python 学习笔记1(序列;if/for/while;函数;类)

    本系列为一个博客的学习笔记,一部分为我原创. 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 1. print 可以打印 有时需要 ...

  3. 流畅python学习笔记:第二十章:属性描述符:

    在前面一章中介绍了@property的用法,但是存在一个问题,如果我有多个属性想转变成property特性,那不是针对每个都需要实现一个 @propery.setter 和 @property.get ...

  4. 流畅python学习笔记:第十四章:迭代器和生成器

    迭代器和生成器是python中的重要特性,本章作者花了很大的篇幅来介绍迭代器和生成器的用法. 首先来看一个单词序列的例子: import re re_word=re.compile(r'\w+') c ...

  5. python 学习笔记(四) 统计序列中元素出现的频度(即次数)

    案例一:在某随机序例中,找到出现频度最高的3个元素,它们出现的次数是多少? from random import randint # 利用列表解析器生成随机序列,包含有30个元素 data = [ra ...

  6. python 学习笔记12(序列常用方法总结)

    http://www.cnblogs.com/vamei/archive/2012/07/19/2599940.html 多回想!!! 1. 序列(list,tuple,string) len(s) ...

  7. python学习笔记19(序列的方法)

    序列包含有宝值 表(tuple)和表(list).此外,字符串(string)是一种特殊的定值表,表的元素可以更改,定值表一旦建立,其元素不可更改. 任何的序列都可以引用其中的元素(item). 下面 ...

  8. 流畅python学习笔记:第十一章:抽象基类

    __getitem__实现可迭代对象.要将一个对象变成一个可迭代的对象,通常都要实现__iter__.但是如果没有__iter__的话,实现了__getitem__也可以实现迭代.我们还是用第一章扑克 ...

  9. 流畅python学习笔记:第十五章:上下文管理器

    在开始本章之前,我们首先来谈谈try-excep..final模块.在Python中,进行异常保护的最多就是用try..except..final.首先来看下下面的代码.进行一个简单的除法运算.为了防 ...

随机推荐

  1. bug 修复:上传库存时前端回发file_name参数丢失;

    # tmp_file = os.path.join(os.path.abspath('youcart/tmp'), json.loads(request.data).get('file_name')) ...

  2. TLS1.0和TLS1.1的区别

    TLS1.1是对TSL1.0的改进其中包括: 改进"抗抵赖"安全特性上的缺陷 完成协议对椭圆曲线的支持,提出了改进的支持ECC算法的传输层安全协议, 握手协议引入了数字签名及验证机 ...

  3. 在ie下,a标签包被img的时候,为什么有个蓝色的边线

    效果像下图这样 那是由于<img>在ie下有默认边框,只要清除边框就可以了,在style中定义 img{ border:none } 显示效果就变成下面这样了 完!

  4. Core ML 机器学习

    在WWDC 2017开发者大会上,苹果宣布了一系列新的面向开发者的机器学习 API,包括面部识别的视觉 API.自然语言处理 API,这些 API 集成了苹果所谓的 Core ML 框架.Core M ...

  5. os模块学习小随笔

    os模块为python解释器与操作系统交互的接口 os.popen()   -------Open a pipe to or from command. The return value is an ...

  6. 放大镜原生js

    <!DOCTYPE html><html><head> <title></title> <style type="text/ ...

  7. TeamViewer——可以实现在手机上随时远程控制你的电脑

    小编今天给大家推荐一款强大的远程控制软件——TeamViewer,可以让你的手机连接你自己的电脑,不管你身处何处,只要电脑和手机都能联网,那么你就可以在手机上控制你的电脑了.以下介绍下如何安装和使用方 ...

  8. 用javascript实现java的Map

    function Map(){ var obj={}; //空的容器 //put方法 this.put=function(key,value){ obj[key]=value; //把键值绑定到obj ...

  9. ThinkPHP3.2 生成二维码

    下面是整合将phpqrcode整合到TP生成二维码就可以解决这个问题了.其实也很简单,使用方法如下:先下载附件解压至ThinkPHP/Extend/Vendor目录,目录不存在自己创建.      v ...

  10. CSS中的尺寸单位

    绝对单位 px: Pixel 像素 pt: Points 磅 pc: Picas 派卡 in: Inches 英寸 mm: Millimeter 毫米 cm: Centimeter 厘米 q: Qua ...