Python惰性评估器

编程入门 行业动态 更新时间:2024-10-07 16:15:09
本文介绍了Python惰性评估器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有Python方式封装惰性函数调用,从而在第一次使用函数f()时调用了先前绑定的函数g(Z),而在后续调用f()中返回了缓存的值?请注意,记忆可能并不完美.

我有:

f = g(Z) if x: return 5 elif y: return f elif z: return h(f)

该代码有效,但我想对其进行重组,以便仅在使用该值的情况下调用g(Z).我不想更改g(...)的定义,并且Z有点大.

我认为f必须是一个函数,但事实并非如此.

解决方案

无论您是寻求缓存还是懒惰的评估,我都有些困惑.对于后者,请查看Alberto Bertogli的模块 lazy.py .

Is there a Pythonic way to encapsulate a lazy function call, whereby on first use of the function f(), it calls a previously bound function g(Z) and on the successive calls f() returns a cached value?

Please note that memoization might not be a perfect fit.

I have:

f = g(Z) if x: return 5 elif y: return f elif z: return h(f)

The code works, but I want to restructure it so that g(Z) is only called if the value is used. I don't want to change the definition of g(...), and Z is a bit big to cache.

EDIT: I assumed that f would have to be a function, but that may not be the case.

解决方案

I'm a bit confused whether you seek caching or lazy evaluation. For the latter, check out the module lazy.py by Alberto Bertogli.

更多推荐

Python惰性评估器

本文发布于:2023-11-28 18:45:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643545.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:惰性   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!