V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MiketsuSmasher
V2EX  ›  Python

[ Python ]如何取回生成器的返回值

  •  
  •   MiketsuSmasher · 2021-09-20 23:11:16 +08:00 · 2041 次点击
    这是一个创建于 941 天前的主题,其中的信息可能已经有所发展或是发生改变。

    以下是一个校验文件的函数,algorithm_name是哈希类型的名称,checksum是已知的校验码。

    def iter_check(filepath: str | bytes | os.PathLike[str | bytes], algorithm_name: str, checksum: str):
        algorithm = get_algorithm(algorithm_name)
        with open(filepath, mode='rb') as file:
            algorithm_instance = algorithm()
            for block in iter(partial(file.read, 1024), b''):
                yield block
                algorithm_instance.update(block)
        return algorithm_instance.hexdigest() == checksum
    

    其本质上是一个生成器,每一次迭代都会yield一个block。那么,能否取回最后由return返回的校验结果?

    3 条回复    2021-09-25 06:53:40 +08:00
    chenstack
        1
    chenstack  
       2021-09-21 00:58:04 +08:00
    生成器迭代结束后会生成一个 StopIteration,返回值就是这个 StopIteration 的 value
    def iter_check():
        for i in range(5):
            yield i
        return 10


    checker = iter_check()
    while True:
        try:
            print(next(checker))
        except StopIteration as e:
            print("result: ", e.value)
            break
    BBCCBB
        2
    BBCCBB  
       2021-09-21 09:41:01 +08:00
    for 循环, 或者看一下 next 函数的用法.
    O5oz6z3
        3
    O5oz6z3  
       2021-09-25 06:53:40 +08:00
    基本原理已经被楼上说完了,至于你的需求可以很容易想到一个简单的实现,不知道有没有现成的:
    class wrapiter:
    ... def __init__(self, _iter):
    ... ... self.iter = iter(_iter)
    ... ... self.val = None
    ... def __iter__(self):
    ... ... return self
    ... def __next__(self):
    ... ... try:
    ... ... ... return next(self.iter)
    ... ... except StopIteration as e:
    ... ... ... self.val = e.value
    ... ... ... raise

    it = wrapiter(range(9))
    print([x for x in it])
    print(it.val)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3780 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 74ms · UTC 00:54 · PVG 08:54 · LAX 17:54 · JFK 20:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.