第四章 面向对象编程 -- 可迭代的对象、迭代器和生成器

第零章 学前准备
第一章 数据结构 – 基本数据类型
第一章 数据结构 – 字符串
第一章 数据结构 – 列表、元组和切片
第一章 数据结构 – 字典
第一章 数据结构 – 集合
第一章 – 数组、队列、枚举
第一章 数据结构 – 序列分类
第二章 控制流程
第三章 函数也是对象 – 函数定义以及参数
第三章 函数也是对象 – 高阶函数以及装饰器
第三章 函数也是对象 – lambda 表达式、可调用函数及内置函数
第四章 面向对象编程 – 自定义类、属性、方法和函数
第四章 面向对象编程–魔术方法1
第四章 面向对象编程 – 魔术方法2
第四章 面向对象编程 – 可迭代的对象、迭代器和生成器


第四章 面向对象编程 – 可迭代的对象、迭代器和生成器

4.5 可迭代的对象、迭代器和生成器

迭代是数据处理的基石。扫描内存中放不下的数据集时,我们要找到一种惰性获取数据项的方式,即按需一次获取一个数据项。这就是迭代器模式( Iterator pattern )。 Python 内置了迭代器模式,可以避免自己手动去实现。

所有生成器都是迭代器,因为生成器完全实现了迭代器的接口。迭代器用于从集合中取出元素;而生成器用于"凭空"生成元素

Python 中,所有序列都可以迭代。在 Python 语言内部,迭代器用于支持:

  • for循环;
  • 构建和扩展集合类型;
  • 逐行遍历文本文件;
  • 列表推导、字典推导和集合推导;
  • 元组拆包;
  • 调用函数时,使用*拆包实参;

4.5.1 为什么Python所有序列都可迭代

之所以所有的序列都可迭代,是因为 iter 内置函数,解释器需要迭代对象时,会自动调用iter(x),内置的 iter 函数有以下作用:

  • 检查对象是否实现了__iter__方法,如果实现了就调用它,获取一个迭代器。
  • 如果没有实现__iter__方法,但是实现了__getitem__方法,Python会创建一个迭代器,尝试按顺序(从索引0开始)获取元素。
  • 如果尝试失败,Python抛出TypeError异常,通常会提示"C object is not iterable"C对象不可迭代),其中C是目标对象所属的类。

任何Python序列都可迭代的原因是,它们都实现了 __getitem__ 方法。

4.5.2 可迭代的对象与迭代器的对比

4.5.2.1 iter 内置函数

使用 iter 内置函数可以获取迭代器的对象。如果对象实现了能返回迭代器的 __iter__ 方法,那么对象就是可迭代的。序列都可以迭代,因为都实现了 __getitem__ 方法,而且其参数是从零开始的索引,这种对象也可以迭代。

我们要明确可迭代的对象和迭代器之间的关系: Python 从可迭代的对象中获取迭代器。如下面的 for 循环,迭代一个字符串。这里,字符串’ABC’是可迭代的对象。背后是有迭代器的,只不过我们看不到。如果我们没有 for 语句,不得不使用 while 循环模拟。

s = "ABC"
for char in s:
    print(char)
print("-"*30)
it = iter(s)  # 1、使用可迭代对象构建迭代器it
while True:
    try:
        print(next(it))  # 2、不断地在迭代器上调用`next`函数获取下一个字符。
    except StopIteration:  # 3、如果没有字符了,迭代器会抛出StopIteration异常。
        del it  # 4、释放对it的引用,即废弃迭代器对象。
        break  # 5、退出循环
A
B
C
------------------------------
A
B
C
4.5.2.2 iterableiterator

标准的迭代器接口有两个方法。

  • __next__返回下一个可用的元素,如果没有元素了,抛出StopIteration异常。
  • __iter__返回self,以便在应该使用可迭代对象的地方使用迭代器,例如在for循环中。

这个接口在 collections.abc.Iterator 抽象基类中制定。这个类定义了 __next__ 抽象方法,而且继承自 Iterable 类; __iter__ 抽象方法则在 Iterable 类中定义。如下图所示。 IterableIterator 抽象基类。以斜体显示的是抽象方法。具体的 Iterable.__iter__ 方法应该返回一个 Iterator 实例。具体的 Iterator 类必须实现 __next__ 方法。 Iterator.__iter__ 方法直接返回实例本身

class Iterator(Iterable):
    __slots__ = ()
    @abstractmethod
    def __next__(self):
        'Return the next item from the iterator. When exhausted, raise StopIteration'
        raise StopIteration
    def __iter__(self):
        return self
    @classmethod
    def __subclasshook__(cls, C):
        if cls is Iterator:
            if (any("__next__" in B.__dict__ for B in C.__mro__) and
                any("__iter__" in B.__dict__ for B in C.__mro__)):
            return True
        return NotImplemented



扫描二维码关注公众号,回复: 17211992 查看本文章
4.5.2.3 Sentence 实例

所以,迭代器是这样的对象:

  • 实现了无参数的 __next__ 方法,返回序列中的下一个元素;
  • 如果没有元素了,那么抛出 StopIteration 异常。 Python 中的迭代器还实现了 __iter__ 方法,因此迭代器也可以迭代。

注意,下面代码是典型地迭代器模式,不过不符合 python 的习惯做法。不过,通过这一版能明确可迭代的集合和迭代器对象之间的关系。定义的 Sentence 类可以迭代,因为它实现了特殊的 __iter__ 方法,构建并返回一个 SentenceIterator 实例。这样,我们就很清楚地看到可迭代的对象和迭代器之间的重要区别,以及二者之间的联系。

注意不要把 Sentence 变成迭代器。要知道,可迭代的对象有个 __iter__ 方法,每次都实例化一个新的迭代器;而迭代器要实现 __next__ 方法,返回单个元素,此外还要实现 __iter__ 方法,返回迭代器本身。因此,迭代器可以迭代,但是可迭代的对象不是迭代器。

除了 __iter__ 方法之外,你可能还想在 Sentence 类中实现 __next__ 方法,让 Sentence 实例既是可迭代的对象,也是自身的迭代器。这样做非常糟糕,因为很多时候,我们需要从同一个可迭代对象中获取多个独立的迭代器,而且各个迭代器要能维护自身的内部状态,因此这一模式正确的实现方式是,每次调用 iter(my_iterable) 都新建一个独立的迭代器。这就是为什么这个示例需要定义 SentenceIterator 类。

import re
import reprlib
RE_WORD = re.compile('\w+')


class Sentence:
    def __init__(self, text):
        self.text = text
        self.words = RE_WORD.findall(text)

    def __repr__(self):
        return 'Sentence(%s)' % reprlib.repr(self.text)

    def __iter__(self):
        return SentenceIterator(self.words)


class SentenceIterator:
    def __init__(self, words):
        self.words = words
        self.index = 0

    def __next__(self):
        try:
            word = self.words[self.index]
        except IndexError:
            raise StopIteration()
        self.index += 1
        return word

    def __iter__(self):
        return self


# 1、传入一个字符串,创建一个Sentence实例
s = Sentence('"The time has come," the Walrus said,')
print(s)  # 2、注意,__repr__方法的输出中包含reprlib.repr方法生成的...。
for word in s:  # 3、Sentence实例可以迭代
    print(word)
print(list(s))  # 4、因为可以迭代,所以Sentence对象可以用于构建列表和其他可迭代的类型
Sentence('"The time ha... Walrus said,')
The
time
has
come
the
Walrus
said
['The', 'time', 'has', 'come', 'the', 'Walrus', 'said']

4.5.3 生成器函数

实现 Sentence 相同功能,但却符合 Python 习惯的方式是,可以用生成器函数代替 SentenceIterator 类。在下面的代码中,迭代器其实是生成器对象,每次调用__iter__方法都会自动创建,因为这里的__iter__方法是生成器函数。

import re
import reprlib
RE_WORD = re.compile('\w+')


class Sentence:
    def __init__(self, text):
        self.text = text
        self.words = RE_WORD.findall(text)

    def __repr__(self):
        return 'Sentence(%s)' % reprlib.repr(self.text)

    def __iter__(self):
        for word in self.words:  # 1、迭代self.words。
            yield word  # 2、产出当前的word。
        return


# 1、传入一个字符串,创建一个Sentence实例
s = Sentence('"The time has come," the Walrus said,')
print(s)  # 2、注意,__repr__方法的输出中包含reprlib.repr方法生成的...。
for word in s:  # 3、Sentence实例可以迭代
    print(word)
print(list(s))  # 4、因为可以迭代,所以Sentence对象可以用于构建列表和其他可迭代的类型
Sentence('"The time ha... Walrus said,')
The
time
has
come
the
Walrus
said
['The', 'time', 'has', 'come', 'the', 'Walrus', 'said']
4.5.3.1 生成器函数的工作原理

只要 Python 函数的定义体中有 yield 关键字,该函数就是生成器函数。调用生成器函数时,会返回一个生成器对象。也就是说,生成器函数是生成器工厂。

PEP 255 – Simple Generators

调用生成器函数返回生成器;生成器产出或生成值。生成器不会以常规的方式"返回"值:生成器函数定义体中的 return 语句会触发生成器对象抛出 StopIteration 异常。

def gen_123():  # 1、只要Python函数中包含关键字yield,该函数就是生成器函数
    yield 1  # 2、生成器函数的定义体中通常都有循环,不过这不是必要条件;这里我重复使用3次yield。
    yield 2
    yield 3


print(gen_123)  # 3、仔细看,gen_123是函数对象。
print(gen_123())  # 4、但是调用时,gen_123( )返回一个生成器对象
for i in gen_123():  # 5、生成器是迭代器,会生成传给yield关键字的表达式的值
    print(i)

g = gen_123()  # 6、为了仔细检查,我们把生成器对象赋值给g。
print(next(g))  # 7、因为g是迭代器,所以调用next(g)会获取yield生成的下一个元素。
print(next(g))
print(next(g))
print(next(g))  # 8、生成器函数的定义体执行完毕后,生成器对象会抛出StopIteration异常。
<function gen_123 at 0x000002489586F040>
<generator object gen_123 at 0x0000024895711040>
1
2
3
1
2
3



---------------------------------------------------------------------------

StopIteration                             Traceback (most recent call last)

Cell In [5], line 15
     13 print(next(g))
     14 print(next(g))
---> 15 print(next(g)) # 8、生成器函数的定义体执行完毕后,生成器对象会抛出StopIteration异常。


StopIteration: 
4.5.3.2 生成器表达式

生成器表达式可以理解为列表推导的惰性版本:不会迫切地构建列表,而是返回一个生成器,按需惰性生成元素。也就是说,如果列表推导是制造列表的工厂,那么生成器表达式就是制造生成器的工厂。

def gen_AB():
    print('start')
    yield 'A'
    print('continue')
    yield 'B'
    print('end.')


# 1、列表推导迫切地迭代gen_AB()函数生成的生成器对象产出的元素:'A'和'B'。注意,同时输出start、continue和end.。
res1 = [x*3 for x in gen_AB()]
print("*"*25)
for i in res1:  # 2、这个for循环迭代列表推导生成的res1列表
    print('-->', i)
print("*"*27)
# 3、把生成器表达式返回的值赋值给res2。只需调用gen_AB()函数,虽然调用时会返回一个生成器,但是这里并不使用。
res2 = (x*3 for x in gen_AB())
print(res2)  # 4、res2是一个生成器对象
# 5、只有for循环迭代res2时,gen_AB函数的定义体才会真正执行。for循环每次迭代时会隐式调用next(res2),前进到gen_AB函数中的下一个yield语句。注意,gen_AB函数的输出与for循环中print函数的输出夹杂在一起。
for i in res2:
    print('-->', i)
start
continue
end.
*************************
--> AAA
--> BBB
***************************
<generator object <genexpr> at 0x00000248956CF820>
start
--> AAA
continue
--> BBB
end.
4.5.3.3 Sentence:惰性实现

设计 Iterator 接口时考虑到了惰性: next(my_iterator) 一次生成一个元素。懒惰的反义词是急迫,其实,惰性求值( lazy evaluation )和及早求值( eager evaluation )是编程语言理论方面的技术术语。目前实现的 Sentence 类不具有惰性,因为 __init__ 方法急迫地构建好了文本中的单词列表,然后将其绑定到 self.words 属性上。这样就得处理整个文本,列表使用的内存量可能与文本本身一样多(或许更多,这取决于文本中有多少非单词字符)。如果只需迭代前几个单词,大多数工作都是白费力气。如下,使用了 RE_WORD.finditer ,省略了 self.words 占用的内存。

import re
import reprlib
import doctest
RE_WORD = re.compile('\w+')


class Sentence:
    """
    1、传入一个字符串,创建一个Sentence实例,注意,__repr__方法的输出中包含reprlib.repr方法生成的...
    >>> s = Sentence('"The time has come," the Walrus said,')
    >>> s
    Sentence('"The time ha... Walrus said,')

    2、Sentence实例可以迭代
    >>> [word for word in s]
    ['The', 'time', 'has', 'come', 'the', 'Walrus', 'said']

    3、因为可以迭代,所以Sentence对象可以用于构建列表和其他可迭代的类型
    >>> list(s)
    ['The', 'time', 'has', 'come', 'the', 'Walrus', 'said']
    """

    def __init__(self, text):
        self.text = text

    def __repr__(self):
        return 'Sentence(%s)' % reprlib.repr(self.text)

    def __iter__(self):
        return (match.group() for match in RE_WORD.finditer(self.text))

if __name__ == "__main__":
    doctest.testmod()

4.6 案例:Vector实例

"""
A multi-dimensional ``Vector`` class, take 8: operator ``==``

A ``Vector`` is built from an iterable of numbers::

    >>> Vector([3.1, 4.2])
    Vector([3.1, 4.2])
    >>> Vector((3, 4, 5))
    Vector([3.0, 4.0, 5.0])
    >>> Vector(range(10))
    Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])


Tests with 2-dimensions (same results as ``vector2d_v1.py``)::

    >>> v1 = Vector([3, 4])
    >>> x, y = v1
    >>> x, y
    (3.0, 4.0)
    >>> v1
    Vector([3.0, 4.0])
    >>> v1_clone = eval(repr(v1))
    >>> v1 == v1_clone
    True
    >>> print(v1)
    (3.0, 4.0)
    >>> octets = bytes(v1)
    >>> octets
    b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
    >>> abs(v1)
    5.0
    >>> bool(v1), bool(Vector([0, 0]))
    (True, False)


Test of ``.frombytes()`` class method:

    >>> v1_clone = Vector.frombytes(bytes(v1))
    >>> v1_clone
    Vector([3.0, 4.0])
    >>> v1 == v1_clone
    True


Tests with 3-dimensions::

    >>> v1 = Vector([3, 4, 5])
    >>> x, y, z = v1
    >>> x, y, z
    (3.0, 4.0, 5.0)
    >>> v1
    Vector([3.0, 4.0, 5.0])
    >>> v1_clone = eval(repr(v1))
    >>> v1 == v1_clone
    True
    >>> print(v1)
    (3.0, 4.0, 5.0)
    >>> abs(v1)  # doctest:+ELLIPSIS
    7.071067811...
    >>> bool(v1), bool(Vector([0, 0, 0]))
    (True, False)


Tests with many dimensions::

    >>> v7 = Vector(range(7))
    >>> v7
    Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
    >>> abs(v7)  # doctest:+ELLIPSIS
    9.53939201...


Test of ``.__bytes__`` and ``.frombytes()`` methods::

    >>> v1 = Vector([3, 4, 5])
    >>> v1_clone = Vector.frombytes(bytes(v1))
    >>> v1_clone
    Vector([3.0, 4.0, 5.0])
    >>> v1 == v1_clone
    True


Tests of sequence behavior::

    >>> v1 = Vector([3, 4, 5])
    >>> len(v1)
    3
    >>> v1[0], v1[len(v1)-1], v1[-1]
    (3.0, 5.0, 5.0)


Test of slicing::

    >>> v7 = Vector(range(7))
    >>> v7[-1]
    6.0
    >>> v7[1:4]
    Vector([1.0, 2.0, 3.0])
    >>> v7[-1:]
    Vector([6.0])
    >>> v7[1,2]
    Traceback (most recent call last):
      ...
    TypeError: 'tuple' object cannot be interpreted as an integer


Tests of dynamic attribute access::

    >>> v7 = Vector(range(10))
    >>> v7.x
    0.0
    >>> v7.y, v7.z, v7.t
    (1.0, 2.0, 3.0)

Dynamic attribute lookup failures::

    >>> v7.k
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 'k'
    >>> v3 = Vector(range(3))
    >>> v3.t
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 't'
    >>> v3.spam
    Traceback (most recent call last):
      ...
    AttributeError: 'Vector' object has no attribute 'spam'


Tests of hashing::

    >>> v1 = Vector([3, 4])
    >>> v2 = Vector([3.1, 4.2])
    >>> v3 = Vector([3, 4, 5])
    >>> v6 = Vector(range(6))
    >>> hash(v1), hash(v3), hash(v6)
    (7, 2, 1)


Most hash codes of non-integers vary from a 32-bit to 64-bit Python build::

    >>> import sys
    >>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
    True


Tests of ``format()`` with Cartesian coordinates in 2D::

    >>> v1 = Vector([3, 4])
    >>> format(v1)
    '(3.0, 4.0)'
    >>> format(v1, '.2f')
    '(3.00, 4.00)'
    >>> format(v1, '.3e')
    '(3.000e+00, 4.000e+00)'


Tests of ``format()`` with Cartesian coordinates in 3D and 7D::

    >>> v3 = Vector([3, 4, 5])
    >>> format(v3)
    '(3.0, 4.0, 5.0)'
    >>> format(Vector(range(7)))
    '(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0)'


Tests of ``format()`` with spherical coordinates in 2D, 3D and 4D::

    >>> format(Vector([1, 1]), 'h')  # doctest:+ELLIPSIS
    '<1.414213..., 0.785398...>'
    >>> format(Vector([1, 1]), '.3eh')
    '<1.414e+00, 7.854e-01>'
    >>> format(Vector([1, 1]), '0.5fh')
    '<1.41421, 0.78540>'
    >>> format(Vector([1, 1, 1]), 'h')  # doctest:+ELLIPSIS
    '<1.73205..., 0.95531..., 0.78539...>'
    >>> format(Vector([2, 2, 2]), '.3eh')
    '<3.464e+00, 9.553e-01, 7.854e-01>'
    >>> format(Vector([0, 0, 0]), '0.5fh')
    '<0.00000, 0.00000, 0.00000>'
    >>> format(Vector([-1, -1, -1, -1]), 'h')  # doctest:+ELLIPSIS
    '<2.0, 2.09439..., 2.18627..., 3.92699...>'
    >>> format(Vector([2, 2, 2, 2]), '.3eh')
    '<4.000e+00, 1.047e+00, 9.553e-01, 7.854e-01>'
    >>> format(Vector([0, 1, 0, 0]), '0.5fh')
    '<1.00000, 1.57080, 0.00000, 0.00000>'


Unary operator tests::

    >>> v1 = Vector([3, 4])
    >>> abs(v1)
    5.0
    >>> -v1
    Vector([-3.0, -4.0])
    >>> +v1
    Vector([3.0, 4.0])


Basic tests of operator ``+``::

    >>> v1 = Vector([3, 4, 5])
    >>> v2 = Vector([6, 7, 8])
    >>> v1 + v2
    Vector([9.0, 11.0, 13.0])
    >>> v1 + v2 == Vector([3+6, 4+7, 5+8])
    True
    >>> v3 = Vector([1, 2])
    >>> v1 + v3  # short vectors are filled with 0.0 on addition
    Vector([4.0, 6.0, 5.0])


Tests of ``+`` with mixed types::

    >>> v1 + (10, 20, 30)
    Vector([13.0, 24.0, 35.0])
    >>> v2d = Vector2d(1, 2)
    >>> v1 + v2d
    Vector([4.0, 6.0, 5.0])


Tests of ``+`` with mixed types, swapped operands::

    >>> (10, 20, 30) + v1
    Vector([13.0, 24.0, 35.0])
    >>> v2d = Vector2d(1, 2)
    >>> v2d + v1
    Vector([4.0, 6.0, 5.0])


Tests of ``+`` with an unsuitable operand:

    >>> v1 + 1
    Traceback (most recent call last):
      ...
    TypeError: unsupported operand type(s) for +: 'Vector' and 'int'
    >>> v1 + 'ABC'
    Traceback (most recent call last):
      ...
    TypeError: unsupported operand type(s) for +: 'Vector' and 'str'


Basic tests of operator ``*``::

    >>> v1 = Vector([1, 2, 3])
    >>> v1 * 10
    Vector([10.0, 20.0, 30.0])
    >>> 10 * v1
    Vector([10.0, 20.0, 30.0])


Tests of ``*`` with unusual but valid operands::

    >>> v1 * True
    Vector([1.0, 2.0, 3.0])
    >>> from fractions import Fraction
    >>> v1 * Fraction(1, 3)  # doctest:+ELLIPSIS
    Vector([0.3333..., 0.6666..., 1.0])


Tests of ``*`` with unsuitable operands::

    >>> v1 * (1, 2)
    Traceback (most recent call last):
      ...
    TypeError: can't multiply sequence by non-int of type 'Vector'


Tests of operator `==`::

    >>> va = Vector(range(1, 4))
    >>> vb = Vector([1.0, 2.0, 3.0])
    >>> va == vb
    True
    >>> vc = Vector([1, 2])
    >>> v2d = Vector2d(1, 2)
    >>> vc == v2d
    True
    >>> va == (1, 2, 3)
    False


Tests of operator `!=`::

    >>> va != vb
    False
    >>> vc != v2d
    False
    >>> va != (1, 2, 3)
    True

"""

import doctest
from array import array
import reprlib
import math
import numbers
import functools
import operator
import itertools


class Vector:
    typecode = 'd'

    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 f'Vector({
      
      components})'

    def __str__(self):
        return str(tuple(self))

    def __bytes__(self):
        return (bytes([ord(self.typecode)]) +
                bytes(self._components))

# tag::VECTOR_V8_EQ[]
    def __eq__(self, other):
        if isinstance(other, Vector):  # <1>
            return (len(self) == len(other) and
                    all(a == b for a, b in zip(self, other)))
        else:
            return NotImplemented  # <2>
# end::VECTOR_V8_EQ[]

    def __hash__(self):
        hashes = (hash(x) for x in self)
        return functools.reduce(operator.xor, hashes, 0)

    def __abs__(self):
        return math.hypot(*self)

    def __neg__(self):
        return Vector(-x for x in self)

    def __pos__(self):
        return Vector(self)

    def __bool__(self):
        return bool(abs(self))

    def __len__(self):
        return len(self._components)

    def __getitem__(self, key):
        if isinstance(key, slice):
            cls = type(self)
            return cls(self._components[key])
        index = operator.index(key)
        return self._components[index]

    __match_args__ = ('x', 'y', 'z', 't')

    def __getattr__(self, name):
        cls = type(self)
        try:
            pos = cls.__match_args__.index(name)
        except ValueError:
            pos = -1
        if 0 <= pos < len(self._components):
            return self._components[pos]
        msg = f'{
      
      cls.__name__!r} object has no attribute {
      
      name!r}'
        raise AttributeError(msg)

    def angle(self, n):
        r = math.hypot(*self[n:])
        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, fmt_spec=''):
        if fmt_spec.endswith('h'):  # hyperspherical coordinates
            fmt_spec = fmt_spec[:-1]
            coords = itertools.chain([abs(self)],
                                     self.angles())
            outer_fmt = '<{}>'
        else:
            coords = self
            outer_fmt = '({})'
        components = (format(c, fmt_spec) for c in coords)
        return outer_fmt.format(', '.join(components))

    @classmethod
    def frombytes(cls, octets):
        typecode = chr(octets[0])
        memv = memoryview(octets[1:]).cast(typecode)
        return cls(memv)

    def __add__(self, other):
        try:
            pairs = itertools.zip_longest(self, other, fillvalue=0.0)
            return Vector(a + b for a, b in pairs)
        except TypeError:
            return NotImplemented

    def __radd__(self, other):
        return self + other

    def __mul__(self, scalar):
        if isinstance(scalar, numbers.Real):
            return Vector(n * scalar for n in self)
        else:
            return NotImplemented

    def __rmul__(self, scalar):
        return self * scalar

    def __matmul__(self, other):
        try:
            return sum(a * b for a, b in zip(self, other))
        except TypeError:
            return NotImplemented

    def __rmatmul__(self, other):
        return self @ other


if __name__ == "__main__":
    doctest.testmod()

猜你喜欢

转载自blog.csdn.net/qq_31654025/article/details/128499628
今日推荐