Python 生成器常用场景一 取代普通迭代器

在上一篇文章

https://blog.csdn.net/nvd11/article/details/138738472

已经简单介绍了生成器 是 一种特殊的迭代器

而的确, 大部分普通的迭代器是可以被生成器取代的, 以达到简化代码的目的。

使用迭代器的例子

我们找回之前介绍迭代器用到的link list 例子:

https://blog.csdn.net/nvd11/article/details/138736024

里面的link list 保护了3个类

Node 类:
python 复制代码
class Node:
    def __init__(self, value):
        self._value = value
        self._next = None
    
    @property
    def value(self):
        return self._value
    
    @property
    def next(self):
        return self._next
    
    @next.setter
    def next(self, next):
        self._next = next

    @value.setter
    def value(self, value):
        self._value = value
LinkList类 它是iterable 可迭代对象
py 复制代码
from loguru import logger

from src.iterator.sample_link_list.link_list_iterator import LinkListIterator
from src.iterator.sample_link_list.node import Node


class LinkList:
    def __init__(self, first) -> None:
        node = Node(first)
        _first_node = node
        _last_node = node

    def __init__(self, *values) -> None:
        if len(values) < 1:
            raise ValueError("At least one node is required")
        self._first_node = Node(values[0])
        current = self._first_node
        for i in range(1, len(values)):
            current.next = Node(values[i])
            current = current.next
        self._last_node = current

    def __iter__(self):
        return LinkListIterator(self._first_node)

    # to print all nodes's value but not nodes themselves
    def print_nodes(self):
        current = self._first_node
        while current:
            logger.info(current.value)
            current = current.next

    def get_length(self):
        current = self._first_node
        count = 0
        while current:
            count += 1
            current = current.next
        return count

    def append(self, value):
        if self.get_length() == 0:
            self._first_node = Node(value)
            self._last_node = self._first_node
        else:
            self._last_node.next = Node(value)
            self._last_node = self._last_node.next
        

        

可以见到, 它的__iter__ 方法返回1个迭代器对象

LinkListIterator(self._first_node)

所以我们还需要被编写这个迭代器的类代码:

它要实现__next__ 方法

python 复制代码
class LinkListIterator:
    def __init__(self, _first_node) -> None:
        self._current_node = _first_node

    def __iter__(self):
        return self
    
    def __next__(self):
        if not self._current_node:
            raise StopIteration
        current = self._current_node
        self._current_node = current.next
        return current.value

改成用生成器

一旦我们改用生成器, 则只需要让LinkList 这个interable 的__iter__ 方法变成1个生成器即可

如何让1个方法/函数变成1个生成器, 上一篇文章讲过, 就是使用yield 关键字啊

修改后的代码如下:

python 复制代码
    def __iter__(self):
        current = self._first_node
        while current:
            yield current.value
            current = current.next
        return

注意这个生成器方法默认是没有参数的

所以它第一元素是 self_first_node

之后yield 当前node 的value出去 , 然后不断让当前元素变成下个原属就好

而这时 原来了的LinkListIterator 就不再需要,大大简化了代码!

相关推荐
IFTICing4 分钟前
【文献阅读】Attention Bottlenecks for Multimodal Fusion
人工智能·pytorch·python·神经网络·学习·模态融合
大神薯条老师8 分钟前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
程序员爱德华10 分钟前
Python环境安装教程
python
huanxiangcoco14 分钟前
152. 乘积最大子数组
python·leetcode
萧鼎44 分钟前
Python常见问题解答:从基础到进阶
开发语言·python·ajax
PythonFun1 小时前
Python技巧:如何避免数据输入类型错误
前端·python
kaikai_sk1 小时前
搜索引擎相关的一段实习经历
python
qinzechen1 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
hakesashou1 小时前
python交互式命令时如何清除
java·前端·python
不写八个1 小时前
Python办公自动化教程(005):Word添加段落
开发语言·python·word