一、基础知识

“__”前缀:私有属性、方法,在__dict__中存储时被改写为“_类名__”前缀

“_”前缀:是约定俗成的保护属性、方法,不过编译器不会对之采取任何处理

二、class特殊方法介绍

在第9章的双元素基础上,我们扩展为任意元素长度的向量,并探讨了相关方法的使用:

__getattr__(self, name):

result = obj.name 会调用builtin函数getattr(obj,'name')查找对应属性,如果没有name属性则调用obj.__getattr__('name')方法,再无则报错

__setattr__(self, name, value):

obj.name = value 会调用builtin函数setattr(obj,'name',value)设置对应属性,如果设置了__setattr__('name',value)方法则优先调用此方法,而非直接将值存入__dict__并新建属性

自己改写时一般添加一些预先处理,最后还是要用到继承super().__setattr__(name, value)

__getitem__(self, item):

在函数体内,会将self[index]的外部表达中的index转化为slice对象,方便处理

__setitem__(self, key, value):

没有用到,用于设置可以self[key]形式查询的数据,即支持序列索引和散列表两种形式

实现程序如下:

import math
import reprlib
from array import array
import numbers # 提供整数基类
import functools
import operator # 亦或方法,虽然'^='功能一样
import itertools class Vector:
typecode = 'd'
shortcut_names = 'xyzt' def __init__(self, components):
self._components = array(self.typecode, components) def __iter__(self):
return iter(self._components) def __repr__(self):
components = reprlib.repr(self._components)
components = components[components.find('['):-1]
return 'Vector({})'.format(components) def __str__(self):
return str(tuple(self)) def __bytes__(self):
return (bytes([ord(self.typecode)]) +
bytes(self._components)) def __abs__(self):
return math.sqrt(sum(x*x for x in self)) def __bool__(self):
return bool(abs(self)) @classmethod
def frombytes(cls, octets):
typecode = chr(octets[0])
memv = memoryview(octets[1:]).cast(typecode)
return cls(memv) # ==========支持切片操作==========
def __len__(self):
return len(self._components) # def __getitem__(self, index):
# # 支持切片类似[1:2]
# # 但是Vector的切片是数组,而非Vector实例
# # 实际上在方法内部index被转化为了slice对象
# return self._components[index] # 和__setitem_对应(此处未实现)
def __getitem__(self, index): # 会将self[index]的index转化为slice对象
cls = type(self)
if isinstance(index, slice):
return cls(self._components[index])
elif isinstance(index, numbers.Integral):
return self._components[index]
else:
raise TypeError('indices must be integers') # ==========动态存取属性==========
# xyzt四个字母分别指代前四个分量,调用如'v.x',
# 设置属性时保证不能使用小写字母a-z为实例添加属性
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]
raise AttributeError('object has no attribute') def __setattr__(self, name, value):
cls = type(self)
if len(name) == 1:
if name in cls.shortcut_names:
error = 'readonly attribute {attr_name!r}'
elif name.islower():
error = "can't set attributes 'a' to 'z' in {cls_name!r}"
else:
error = '' # bool('')=False
if error:
raise AttributeError(error.format(cls_name=cls.__name__,
attr_name=name))
super().__setattr__(name, value) # ==========哈希方法==========
def __hash__(self):
# hashes = map(hash, self._components)
hashes = (hash(x) for x in self._components)
return functools.reduce(operator.xor, hashes, initial=0)
# reduce的initial表示初始值,如果序列为空则返回初始值,否则
# 初始值加到序列首,对亦或运算初始值应该为0 def __eq__(self, other):
# ----------version1----------
# 原版(注释句)在向量很长时消耗内存过巨,需要构建两个元组
# return tuple(self) == tuple(other)
# ----------version2----------
# if len(self) != len(other):
# return False
# for a, b in zip(self, other):
# if a != b:
# return False
# return True
# ----------version3----------
return len(self) == len(other) and \
all(a == b for a, b in zip(self, other)) # format规范化输出
def angle(self, n):
r = math.sqrt(sum(x * x for x in self._components))
a = math.atan2(r, self[n-1])
if (n == len(self) - 1) and (self[-1] < 0):
return math.pi * 2 - a
else:
return a def angles(self):
return (self.angle(n) for n in range(1, len(self))) def __format__(self, format_spec=''):
if format_spec.endswith('h'):
format_spec = format_spec[:-1]
# r, angle1,angle2,……
coords = itertools.chain([abs(self)], self.angles())
out_fmt = '<{}>'
else:
coords = self
out_fmt = '({})'
components = (format(c, format_spec) for c in coords)
return out_fmt.format(','.join(components)) if __name__ == "__main__":
print(Vector(range(10)))
v = Vector(range(7))
print(v)
print(v[3])
print(v.x)

『流畅的Python』第10章笔记_序列类型的更多相关文章

  1. 『流畅的Python』第9章笔记_对象

    一.Python风格 以一个二元素向量对象为例 import math from array import array class Vector2d: typecode = 'd' def __ini ...

  2. 『流畅的Python』第5章笔记_一等函数

  3. 『流畅的Python』第15章:上下文管理器和else块

  4. 『流畅的Python』第14章:可迭代的对象、迭代器和生成器

  5. 『流畅的Python』第13章:正确重载运算符

  6. 『流畅的Python』第12章:继承的优缺点

  7. 『流畅的Python』第1~4章笔记_数据结构、编码

    由于1~4章内容零散且基础,所以统计一下涉及到的内容,记录一下,方便查阅(第一张图右键新页面打开即可看到清晰大图)

  8. 《流畅的Python》Data Structures--第2章序列array

    第二部分 Data Structure Chapter2 An Array of Sequences Chapter3 Dictionaries and Sets Chapter4 Text vers ...

  9. Python基本语法_基本数据类型_序列类型详解

    目录 目录 序列 序列的标准操作符 切片操作符 一个例子 字符串的连接 序列的功能函数 enumerate 枚举出序列对象的元素 len 获取序列对象的长度 min 取出sequence中的最小值 m ...

随机推荐

  1. 【问题解决:时区】连接MySQL时错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone

    问题描述: MySQL升级到8.0.11之后连接数据库报错: Your login attempt was not successful, try again. Reason: Could not g ...

  2. 远程主动读取数据 RFC_READ_TABLE

    IF IM_UDATE1 <> IM_UDATE2."get data from bw     CLEAR IT_SEL_TAB.     IT_SEL_TAB = '( /BI ...

  3. php的Allowed memory size of 134217728 bytes exhausted问题

    提示Allowed memory size of 134217728 bytes exhausted,出现这种错误的情况常见的有三种: 0:查询的数据量大. 1:数据量不大,但是php.ini配置的内 ...

  4. ASP.NET —— Web Pages

    为简单起见,新建一个空的web工程,再新建一个MVC的视图(.cshtml),因为WP是单页面模型,所以以后就在这个页面中进行试验. Razor语法简介: 变量可用var或者其确切类型声明. 遍历fo ...

  5. 批量Excel数据导入Oracle数据库 导入excel错误:外部表不是预期的格式 解决方案

    在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一下,导出的Excel是标准文件不是html,没错 ...

  6. node.js基本概念简单解释

    1:什么是回调函数? 2:什么是同步异步 3:什么是I/O 4:什么是单线程/多线程 5:什么是阻塞/非阻塞 6:什么是事件 7:什么是事件驱动 8:什么是事件驱动的回调 9:什么是事件循环 解释: ...

  7. 【BZOJ】1832: [AHOI2008]聚会

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1832 省选出出了CF的感觉..... 显然一发贪心,如果两个点显然就是他们的$LCA$(不 ...

  8. 【BZOJ】3572: [Hnoi2014]世界树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3572 算是细节题了吧.. 构出虚树,考虑z正反DP两次求出虚树中每个点最近的议事处是哪一个 ...

  9. 用html+css+js实现选项卡切换效果

    文章转载自:http://tongling.github.io/JSCards/ 用html+css+js实现选项卡切换效果 使用之前学过的综合知识,实现一个新闻门户网站上的常见选项卡效果: 文字素材 ...

  10. WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.

    点击报错信息中的app, 按照提示,修改compile 为 implementation 再次同步即可 结果