从可呼叫功能返回值
# -*- coding: UTF-8 -*-
#Sentience
import typing
Ninf = typing.Callable[[int], bool]
Ninf_map = typing.Callable[[Ninf], bool]
def inj(k: int) -> Ninf:
return lambda n: n < k
def eps(p : Ninf_map) -> Ninf:
return lambda n: min(p(inj(k)) for k in range(n + 1))
def omniscience(p : Ninf_map) -> typing.Optional[Ninf]:
return None if p(x := eps(p)) else x
inj
eps
omniscience
s = x
if s:
print("XOXO")
如您在这里所见,X没有价值,但是在函数“无所不知”中,它已分配。
在这种情况下,我想知道如何检索“ X”…
# -*- coding: UTF-8 -*-
#Sentience
import typing
Ninf = typing.Callable[[int], bool]
Ninf_map = typing.Callable[[Ninf], bool]
def inj(k: int) -> Ninf:
return lambda n: n < k
def eps(p : Ninf_map) -> Ninf:
return lambda n: min(p(inj(k)) for k in range(n + 1))
def omniscience(p : Ninf_map) -> typing.Optional[Ninf]:
return None if p(x := eps(p)) else x
inj
eps
omniscience
s = x
if s:
print("XOXO")
As you see here, x has no value, but in function “omniscience” it is assigned.
I would like to know how to retrieve “x” in this case…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需用论点调用函数全知
Just call the function omniscience with an argument
简单的答案是“ Call
Omniscience
具有适当的参数”。在运行时,p
可以是 ,因为您实际上没有使用它。如果您要执行静态类型检查,则p
必须是一个函数(或更确切地说,您可以调用的内容),即使p
从未被调用或使用。 (类型Checkers仅担心匹配签名,而不是运行时行为。)让我们看一下
ninf
:它基本上是整数上的谓词。某些示例可以命名甚至
,奇数
,greate_than_zero
等。false
基于数字的某些属性。让我们看一下
ninf_map
。它是另一个谓词,但这一次是在谓词本身上。我们的抽象水平更难掌握。我们可以在谓词上定义什么样的谓词?我们不能真正“检查”谓词;我们所能做的就是在某些参数上称其为“它在每种情况下都返回的内容,这都是一个微不足道的示例。
(请注意,
令人满意的
永远无法返回false
;它返回true
或它永远运行。但是, type 没关系,这就是我们这里关心的。 /code>,不管
omniscience
是否使用它。The simple answer is "call
omniscience
with an appropriate argument". At runtime,p
can be anything, since you don't actually use it. If you are performing static type-checking,p
has to be a function (or more precisely, something you can call), even thoughp
is never called or used. (Type checkers only worry about matching signatures, not about runtime behavior.)Let's look at
Ninf
: it's basically a predicate on integers. Some examples might be namedeven
,odd
,greater_than_zero
, etc. It takes a number, and returnsTrue
orFalse
based on some property of the number.Let's take a look at
Ninf_map
. It's another predicate, but this time on a predicate itself. We're at a level of abstraction that's a little harder to grasp. What kind of predicates can we define on a predicate? We can't really "examine" the predicate; all we can do is call it on some arguments and see what it returns in each caseHere's a trivial example.
(Note that
satisfiable
can never returnFalse
; it either returnsTrue
or it runs forever. But, the type is OK, which is all we care about here.)Because
satisfiable
has typeCallable[[Ninf],bool]
, it is a valid argument toomniscience
, regardless of whetheromniscience
uses it or not.