- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我定义了一个函数f(x)
,它在语法上是正确的。我想确保 f
可以“正常运行”,因为它的输出完全取决于输入 x
。
有时候,如果f
的定义很复杂,可能会无意中(因为错别字,或者不够小心)在它的定义中引用了一些外部变量,这会导致错误很难找到。
是否有任何工具、任何“指令 pragma”(可以这么说)或任何“最佳实践”来确保不会发生这种情况?
例子:
xx = 1.0
def f(x, y, p=1):
return xx * y**p # A typo here: should be x * y**p
如果在python中没有简单的方法可以实现,哪些语言有这样的功能?据我所知,C、C++和Fortran没有这个。 Fortran 95 有纯子程序和函数,但它被程序员用来“ promise ”一个子程序/函数不会修改任何外部变量,而子程序/函数仍然可以从它们中取值。
最佳答案
虽然有点 hackish,但您可以做这样的事情来检查函数代码对象中使用的指令:
import opcode # see /Python/Lib/opcode.py
GLOBAL_INSTRUCTIONS = {opcode.opmap['DELETE_GLOBAL'],
opcode.opmap['LOAD_GLOBAL'],
opcode.opmap['STORE_GLOBAL']}
def is_pure(func):
for inst in instructions(func.func_code.co_code):
op = inst[0]
if op in GLOBAL_INSTRUCTIONS:
return False
return True
def instructions(code):
"""Iterates over a code string yielding integer [op, arg] pairs
"""
code = map(ord, code)
i, L = 0, len(code)
extended_arg = 0
while i < L:
op = code[i]
i+= 1
if op < opcode.HAVE_ARGUMENT:
yield [op, None]
continue
oparg = code[i] + (code[i+1] << 8) + extended_arg
extended_arg = 0
i += 2
if op == opcode.EXTENDED_ARG:
extended_arg = oparg << 16
continue
yield [op, oparg]
xx = 1.0
def f(x, y, p=1):
return xx * y**p # A typo here: should be x * y**p
def f2(x, y, p=1):
return x * y**p # No typo
print(is_pure(f)) # --> False
print(is_pure(f2)) # --> True
关于python - 如何确保 python 函数不依赖于外部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28948146/
我想知道如果一个人需要并行处理项目 A、B 和 C,其中 A 依赖于 B,B 依赖于 C,那么完美的工作流程是什么。 目前,我将所有内容都放在一个存储库中,这加快了早期开发速度。所以我的工作目录如下所
尝试测试这款外观精美的 gem - http://icelab.com.au/articles/welcome-to-the-omnisocial/ - 这 promise 可以将 Twitter 和
因为每个版本flutter_localizations来自 SDK 取决于 intl 0.17.0而 fstore 依赖于 intl ^0.16.1 , 禁止来自 SDK 的 flutter_loca
类似于Typescript: Type of a property dependent on another property within the same object我想要一种属性依赖的类型。
我需要计算分页的结果数。 演示查询 select A.order_id, IF( E.assign_date IS NOT NULL AND E.assign_dat
我的表中有一个项目列表,其中包含字段 votes_up 和 votes_down 以及其他字段。现在我想根据这两个字段的功能根据受欢迎程度订购商品。问题在于受欢迎程度的算法取决于 votes_up 的
cloud_firestore:^0.14.4firebase_auth: ^0.18.4+1smooth_star_rating: ^1.0.4地理点:^0.7.1geoflutterfire: ^
是否可以定义两个不同的 PatternLayouts 并依赖于它使用第一个或第二个的应用程序 LogLevel? 示例: 如果我使用默认的 ERROR 级别运行我的应用程序,它应该打印如下日志语句:
我有一个函数 template void frobnicate()做的东西。我需要 T 成为少数几个选择类型之一,并且我需要一些关于这些类型的信息。我通过提供特征来做到这一点: template st
我目前正在开发我的一个项目,它是一个类似于 MSPaint 的 WPF 应用程序。但是,我不使用铅笔工具或类似的工具,而是使用对象(矩形、圆形、三角形等)进行绘画。我使用 Prism 和 MVVM 模
我在 ftable 周围写了一个包装器因为我需要计算许多变量的频率和百分比的平面表: mytable <- function(...) { tab <- ftable(...,
如果可以在 Gradle 中使用来自其他项目的任务,我想尝试一下。假设 ProjectB 是一个 src/main/groovy 包含 com.MyTask 的项目,有父 ProjectA 在 Pro
我需要测试一些依赖于当前上下文的静态方法。现在,我当然可以使用 HttpContextWrapper 从我自己的代码中删除这种依赖性。问题在于我在这些方法中使用的第 3 方 API。他们依赖于 Htt
我正在尝试创建一个通用的结构,具有通用实现特征的界限。特征本身是通用的。这是在 Rust 1.49.0 中。 如果我这样做: trait Foo {} struct Baz> { x: F,
如果 Makefile 本身被更改,一个安全的赌注是将所有目标视为过时的。 有没有聪明的方法来添加这种依赖?有没有其他选择? 最佳答案 一个安全的赌注,但一个可怕的想法。示例:您正在使用 automa
我有一种情况,我需要根据远程文件在 make 中执行规则。这是我正在尝试做的一个例子(实际的 Makefile 在这里不相关的方式要复杂得多): URL = http://cdn.sstatic.ne
这是我的第一个 Spring 应用程序,所以请原谅我对此事的无知。 我在 @Autowired 依赖项上遇到 NullPoinerException。 14:08:48,415 SEVERE [com
我在编程方面没有太多经验,所以这是我的问题: 我正在尝试编写一个转换器应用程序。最后,您可以输入一个数字。然后我有两个组件UIPickerView .使用第一个组件,您可以选择输入格式(例如 °Cel
假设我在 Haxe 中有以下类: class Pair { public var first:U = null; public var second:V = null; pub
在很多与向服务器发送请求相关的问题的回答中,我看到提倡使用 Javascript/AJAX。 从某种意义上说,我仍然是一个纯粹主义者,因为我首先尝试开发无需使用 Javascript/AJAX 即可完
我是一名优秀的程序员,十分优秀!