{"id":1149673,"date":"2025-01-13T16:52:25","date_gmt":"2025-01-13T08:52:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149673.html"},"modified":"2025-01-13T16:52:27","modified_gmt":"2025-01-13T08:52:27","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e8%bf%ad%e4%bb%a3%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149673.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efa\u8fed\u4ee3\u5668"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180328\/ed116907-f1c4-46a7-a36f-7e1f7541742c.webp\" alt=\"python\u5982\u4f55\u521b\u5efa\u8fed\u4ee3\u5668\" \/><\/p>\n<p><p> <strong>Python\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u3001\u751f\u6210\u5668\u51fd\u6570\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong>\uff0c\u5176\u4e2d\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u8f83\u4e3a\u5e38\u89c1\u548c\u6613\u7528\u3002\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u4fdd\u5b58\u5f53\u524d\u72b6\u6001\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u6765\u521b\u5efa\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u503c\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8bb0\u4f4f\u4e0a\u4e00\u6b21\u7684\u6267\u884c\u72b6\u6001\u3002\u76f8\u8f83\u4e8e\u4f20\u7edf\u7684\u8fd4\u56de\u503c\u51fd\u6570\uff0c\u751f\u6210\u5668\u51fd\u6570\u5728\u751f\u6210\u5927\u91cf\u6570\u636e\u65f6\u66f4\u8282\u7701\u5185\u5b58\u3002\u56e0\u6b64\uff0c\u751f\u6210\u5668\u51fd\u6570\u5728\u5904\u7406\u5927\u6570\u636e\u6216\u6d41\u5f0f\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\u7c7b<\/h3>\n<\/p>\n<p><h4>1.1 \u5b9a\u4e49\u8fed\u4ee3\u5668\u7c7b<\/h4>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u8fed\u4ee3\u5668\u7c7b\uff0c\u9996\u5148\u9700\u8981\u5b9e\u73b0\u4e24\u4e2a\u65b9\u6cd5\uff1a<code>__iter__()<\/code>\u548c<code>__next__()<\/code>\u3002<code>__iter__()<\/code>\u65b9\u6cd5\u8fd4\u56de\u8fed\u4ee3\u5668\u5bf9\u8c61\u672c\u8eab\uff0c<code>__next__()<\/code>\u65b9\u6cd5\u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u503c\u3002\u5982\u679c\u6ca1\u6709\u66f4\u591a\u7684\u503c\u53ef\u4ee5\u8fd4\u56de\uff0c<code>__next__()<\/code>\u65b9\u6cd5\u5e94\u8be5\u629b\u51fa<code>StopIteration<\/code>\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyIterator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.index = 0<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.index &lt; len(self.data):<\/p>\n<p>            result = self.data[self.index]<\/p>\n<p>            self.index += 1<\/p>\n<p>            return result<\/p>\n<p>        else:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>MyIterator<\/code>\u5bf9\u8c61\u5e76\u4f7f\u7528\u5b83\u6765\u904d\u5386\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_data = [1, 2, 3, 4, 5]<\/p>\n<p>iterator = MyIterator(my_data)<\/p>\n<p>for value in iterator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u503c\u3002\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u4ece\u4e0a\u6b21\u79bb\u5f00\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6bcf\u6b21\u8c03\u7528\u65f6\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u751f\u6210\u5668\u5bf9\u8c61\u5e76\u4f7f\u7528\u5b83\u6765\u904d\u5386\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_data = [1, 2, 3, 4, 5]<\/p>\n<p>generator = my_generator(my_data)<\/p>\n<p>for value in generator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u751f\u6210\u5668\uff0c\u5b83\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b9a\u4e49\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u751f\u6210\u5668\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_data = [1, 2, 3, 4, 5]<\/p>\n<p>generator = (item for item in my_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u904d\u5386\u751f\u6210\u5668\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for value in generator:<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u6811\u6216\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u904d\u5386\u6811\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b9a\u4e49\u6811\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u6811\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u4e00\u4e2a\u503c\u548c\u4e00\u4e2a\u5b50\u8282\u70b9\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.children = []<\/p>\n<p>    def add_child(self, child_node):<\/p>\n<p>        self.children.append(child_node)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u5b9a\u4e49\u6811\u904d\u5386\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u6765\u904d\u5386\u6811\u7684\u8282\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def tree_generator(node):<\/p>\n<p>    yield node.value<\/p>\n<p>    for child in node.children:<\/p>\n<p>        yield from tree_generator(child)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3 \u4f7f\u7528\u6811\u904d\u5386\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u6811\u5e76\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u904d\u5386\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">root = TreeNode(1)<\/p>\n<p>child_1 = TreeNode(2)<\/p>\n<p>child_2 = TreeNode(3)<\/p>\n<p>root.add_child(child_1)<\/p>\n<p>root.add_child(child_2)<\/p>\n<p>child_1.add_child(TreeNode(4))<\/p>\n<p>child_1.add_child(TreeNode(5))<\/p>\n<p>for value in tree_generator(root):<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u65e0\u9650\u5e8f\u5217<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65e0\u9650\u5e8f\u5217\uff0c\u4f8b\u5982\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u751f\u6210\u5668\u51fd\u6570\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u5b9a\u4e49\u6590\u6ce2\u90a3\u5951\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u6765\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci_generator():<\/p>\n<p>    a, b = 0, 1<\/p>\n<p>    while True:<\/p>\n<p>        yield a<\/p>\n<p>        a, b = b, a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528\u6590\u6ce2\u90a3\u5951\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u751f\u6210\u5668\u5bf9\u8c61\u5e76\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fib_gen = fibonacci_generator()<\/p>\n<p>for _ in range(10):<\/p>\n<p>    print(next(fib_gen))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u6587\u4ef6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u6570\u636e\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5e76\u8fd4\u56de\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u7684\u751f\u6210\u5668\u51fd\u6570\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u5b9a\u4e49\u6587\u4ef6\u8bfb\u53d6\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u6765\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def file_reader(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield line.strip()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5e76\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for line in file_reader(&#39;example.txt&#39;):<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u5e76\u53d1\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u4e0e<code>asyncio<\/code>\u5e93\u7ed3\u5408\u4f7f\u7528\u6765\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u5b9a\u4e49\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570\u6765\u5f02\u6b65\u751f\u6210\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def async_generator():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>        yield i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u4f7f\u7528\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5f02\u6b65\u751f\u6210\u5668\u51fd\u6570\u5e76\u5f02\u6b65\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def main():<\/p>\n<p>    async for value in async_generator():<\/p>\n<p>        print(value)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u7ec4\u5408\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u5f7c\u6b64\u7ec4\u5408\uff0c\u4ee5\u521b\u5efa\u590d\u6742\u7684\u6570\u636e\u751f\u6210\u7ba1\u9053\u3002\u4ee5\u4e0b\u662f\u7ec4\u5408\u751f\u6210\u5668\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u5b9a\u4e49\u7ec4\u5408\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e24\u4e2a\u751f\u6210\u5668\u51fd\u6570\u5e76\u5c06\u5b83\u4eec\u7ec4\u5408\u5728\u4e00\u8d77\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator1(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p>def generator2(data):<\/p>\n<p>    for item in generator1(data):<\/p>\n<p>        yield item * 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u4f7f\u7528\u7ec4\u5408\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u7ec4\u5408\u751f\u6210\u5668\u51fd\u6570\u5904\u7406\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>for value in generator2(data):<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u751f\u6210\u5668\u51fd\u6570\u7684\u6027\u80fd\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u5728\u751f\u6210\u5927\u91cf\u6570\u636e\u65f6\u5177\u6709\u663e\u8457\u7684\u6027\u80fd\u4f18\u52bf\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u9700\u8981\u5c06\u6240\u6709\u6570\u636e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6bd4\u8f83\u751f\u6210\u5668\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u6027\u80fd\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>9.1 \u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u548c\u4e00\u4e2a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator_function(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p>def list_comprehension(data):<\/p>\n<p>    return [item for item in data]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.2 \u6bd4\u8f83\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6bd4\u8f83\u751f\u6210\u5668\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>data = range(1000000)<\/p>\n<p>gen_time = timeit.timeit(lambda: list(generator_function(data)), number=10)<\/p>\n<p>list_time = timeit.timeit(lambda: list_comprehension(data), number=10)<\/p>\n<p>print(f&#39;Generator function time: {gen_time}&#39;)<\/p>\n<p>print(f&#39;List comprehension time: {list_time}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u751f\u6210\u5668\u51fd\u6570\u7684\u8c03\u8bd5\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u7684\u8c03\u8bd5\u548c\u6d4b\u8bd5\u4e0e\u666e\u901a\u51fd\u6570\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u7684\u5355\u5143\u6d4b\u8bd5\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>10.1 \u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\u548c\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u548c\u76f8\u5e94\u7684\u5355\u5143\u6d4b\u8bd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def simple_generator(data):<\/p>\n<p>    for item in data:<\/p>\n<p>        yield item<\/p>\n<p>import unittest<\/p>\n<p>class TestSimpleGenerator(unittest.TestCase):<\/p>\n<p>    def test_generator(self):<\/p>\n<p>        data = [1, 2, 3]<\/p>\n<p>        gen = simple_generator(data)<\/p>\n<p>        self.assertEqual(next(gen), 1)<\/p>\n<p>        self.assertEqual(next(gen), 2)<\/p>\n<p>        self.assertEqual(next(gen), 3)<\/p>\n<p>        with self.assertRaises(StopIteration):<\/p>\n<p>            next(gen)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u751f\u6210\u5668\u51fd\u6570\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5e7f\u6cdb\u7684\u7528\u9014\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u6d41\u5904\u7406<\/strong>\uff1a\u5904\u7406\u5b9e\u65f6\u6570\u636e\u6d41\uff0c\u4f8b\u5982\u4f20\u611f\u5668\u6570\u636e\u3001\u65e5\u5fd7\u6570\u636e\u7b49\u3002<\/li>\n<li><strong>\u5927\u6570\u636e\u5904\u7406<\/strong>\uff1a\u9010\u6761\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u5230\u5185\u5b58\u3002<\/li>\n<li><strong>\u7f51\u7edc\u722c\u866b<\/strong>\uff1a\u9010\u9875\u6293\u53d6\u7f51\u9875\u6570\u636e\uff0c\u907f\u514d\u8fc7\u591a\u5360\u7528\u7f51\u7edc\u8d44\u6e90\u3002<\/li>\n<li><strong>\u56fe\u5904\u7406<\/strong>\uff1a\u904d\u5386\u56fe\u7684\u8282\u70b9\u548c\u8fb9\uff0c\u8fdb\u884c\u56fe\u7b97\u6cd5\u7684\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u591a\u4efb\u52a1\u5904\u7406<\/strong>\uff1a\u7ed3\u5408\u5f02\u6b65\u751f\u6210\u5668\u548c<code>asyncio<\/code>\u5e93\uff0c\u5904\u7406\u5e76\u53d1\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u7406\u89e3\u548c\u638c\u63e1\u751f\u6210\u5668\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u53ef\u4ee5\u7f16\u5199\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u4ee3\u7801\u6765\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u751f\u6210\u548c\u5904\u7406\u4efb\u52a1\u3002\u751f\u6210\u5668\u51fd\u6570\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\u7ed3\u6784\uff0c\u8fd8\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\uff1f<\/strong><br \/>\u8981\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5e76\u5b9e\u73b0<code>__iter__()<\/code>\u548c<code>__next__()<\/code>\u4e24\u4e2a\u65b9\u6cd5\u3002<code>__iter__()<\/code>\u65b9\u6cd5\u8fd4\u56de\u8fed\u4ee3\u5668\u5bf9\u8c61\u672c\u8eab\uff0c\u800c<code>__next__()<\/code>\u65b9\u6cd5\u8fd4\u56de\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u503c\uff0c\u5e76\u5728\u6ca1\u6709\u66f4\u591a\u503c\u65f6\u5f15\u53d1<code>StopIteration<\/code>\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8fed\u4ee3\u5668\u6765\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002<\/p>\n<p><strong>\u4f7f\u7528\u751f\u6210\u5668\u4e0e\u8fed\u4ee3\u5668\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u751f\u6210\u5668\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u8fed\u4ee3\u5668\uff0c\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u751f\u6210\u503c\u800c\u4e0d\u9700\u8981\u5b9a\u4e49\u7c7b\u548c\u5b9e\u73b0\u65b9\u6cd5\u3002\u4e0e\u4f20\u7edf\u8fed\u4ee3\u5668\u76f8\u6bd4\uff0c\u751f\u6210\u5668\u66f4\u6613\u4e8e\u7f16\u5199\u548c\u7406\u89e3\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5185\u90e8\u7ef4\u62a4\u72b6\u6001\u5e76\u80fd\u81ea\u52a8\u5904\u7406<code>StopIteration<\/code>\u5f02\u5e38\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u8fed\u4ee3\u9700\u6c42\uff0c\u751f\u6210\u5668\u901a\u5e38\u662f\u66f4\u4f18\u96c5\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684\u8fed\u4ee3\u5668\uff1f<\/strong><br \/>Python\u5185\u7f6e\u4e86\u591a\u79cd\u8fed\u4ee3\u5668\uff0c\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u548c\u96c6\u5408\u7b49\u3002\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u672c\u8eab\u90fd\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6216<code>next()<\/code>\u51fd\u6570\u8f7b\u677e\u904d\u5386\u5b83\u4eec\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u7684\u8fed\u4ee3\u5668\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u96c6\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u521b\u5efa\u8fed\u4ee3\u5668\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u7c7b\u3001\u751f\u6210\u5668\u51fd\u6570\u3001\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff0c\u5176\u4e2d\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u8f83\u4e3a\u5e38\u89c1\u548c\u6613\u7528\u3002\u751f [&hellip;]","protected":false},"author":3,"featured_media":1149679,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149673"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1149673"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149673\/revisions"}],"predecessor-version":[{"id":1149680,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149673\/revisions\/1149680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}