0
0

More than 3 years have passed since last update.

一部処理の他ジェネレーターへの委譲

Posted at

複雑なジェネレーターを記述する際には、yield from命令を利用することで処理を分離でき、コードがより見通しやすくなる。

gen_from.py
def read_files(*files):
    for file in files:
        yield from read_lines(file)

def read_lines(path):
    with open(path,'r',encoding='UTF-8') as file:
        for line in file:
            yield line.rstrip('\n')

for line in read_files("./path/1","./path/2","./path/3"):
    print(line)

独習Pthon 9.2.4 より引用

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0