您能否建议一种支持在运行时修改代码的强大语言或功能?
这就是我在运行时修改代码的意思:
Start:
a=10,b=20,c=0;
label1: c=a+b;
....
label1= c=a*b;
goto label1;
Run Code Online (Sandbox Code Playgroud)
并且可能正在建立一份说明清单:
code1.add(c=a+b);
code1.add(c=c*(c-1));
code1. execute();
Run Code Online (Sandbox Code Playgroud) 我一直在阅读球拍指南中的合同.
该->i构造允许在函数的输入/输出上放置任意约束.
例如,我可以使用一个unzip函数来获取对列表并返回两个列表.使用契约,我可以确认列表中的每个元素都是一对,并且外部列表具有相应的元素.
球拍指南提示这是合同有用的时候.但似乎在函数本身内做得更好.如果遇到非配对,我可能会抛出错误,这会检查列表中的内容.通过具有正确的功能自动检查输出.
什么是通过比简单类型更复杂的契约以某种方式改进代码的具体示例?
我需要从CLRS算法书中获得这个练习的提示:
证明无论我们在高度-h二进制搜索树中从哪个节点开始,对Tree-Successor的k次连续调用都需要O(k + h)时间.
我想使用多个通用协议并确保它们兼容:
from typing import TypeVar, Protocol, Generic
from dataclasses import dataclass
# checking fails as below and with contravariant=True or covariant=True:
A = TypeVar("A")
class C(Protocol[A]):
def f(self, a: A) -> None: pass
class D(Protocol[A]):
def g(self) -> A: pass
# Just demonstrates my use case; doesn't have errors:
@dataclass
class CompatibleThings(Generic[A]):
c: C[A]
d: D[A]
Run Code Online (Sandbox Code Playgroud)
Mypy 出现以下错误:
Invariant type variable 'A' used in protocol where contravariant one is expected
Invariant type variable 'A' used in protocol where covariant one …Run Code Online (Sandbox Code Playgroud) Javascript符号提供简单的唯一标识符:
> const foo1 = Symbol('foo');
> foo1
Symbol(foo)
> const foo2 = Symbol('foo');
> foo1 === foo2
false
> let m = new Map();
> m.set(foo1, "bar");
> m.set(foo2, "rosco");
> m.get(foo1)
'bar'
> m.get(foo2)
'rosco'
Run Code Online (Sandbox Code Playgroud)
这对于例如当您不能使用Exceptions 时从具有特殊含义的函数获得唯一的返回值很有用。
我知道在 python 中你可以使用object()或随机数,但是当你打印它们时,这些都不会表现得很好。
Symbolpython中有类似javascript的东西吗?