[ Python ]如何取回生成器的返回值
source link: https://www.v2ex.com/t/803110
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
以下是一个校验文件的函数,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
返回的校验结果?
Recommend
-
52
列表生成式一个小题目:将里列表[0,1,2,3]里面的数值都加1.方法1:a=[0,1,2,3]b=[]foriinrange(len(a)):b.append(i+1)a=bprint(a)方法2:a=[1,3,4,6,7,7,8]forindex,iinenumerate(a):a[index]+=1print(a)方法3:a=[0,1,2,3,4]a=map(lambdax:x+1,a)pr
-
105
简单例子讲解python里的生成器
-
72
点击上方“Python开发”,选择“置顶公众号” 关键时刻,第一时间送达! ...
-
11
What is Generator PEP 255 -- Simple Generators 原文链接。 Python 中的 generator 有两类,generator function 和 generator expressions。Genrator Function 实际上是一个工厂函...
-
9
Fluent Python 笔记 —— 可迭代对象、迭代器和生成器 2020-10-18 ...
-
9
Python生成器详解 Python一对一答疑,帮助有志青年!使用QQ在线辅导,哪里不懂问哪里,整个过程都是一对一,学习更有针对性。和作者直接交流,不但提升技能,还提升 Level;当你决定加入...
-
13
Python生成器的一个坑 用Python写迭代器(Iterator)的时候,可能会有人向你推荐生成器(Generator),并列举一堆生成器的好处。不过,今天要来分享一个生成器的坑……...
-
5
连Python生成器(Generator)的原理都解释不了,还敢说Python用了5年? ...
-
3
瑞幸咖啡垂死回生,可以高枕无忧了吗? 13小时前...
-
4
Python编程:如何搞定生成器(Generator)及表达式?来盘它! 作者:传新视界 2022-10-27 13:58:32 主要介绍了生成器相关知识,用于更好的自定义迭代器。内容包括何为生成器?如何自定义生成器以及和普通函数的关...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK