从可呼叫功能返回值

发布于 2025-01-25 12:37:48 字数 526 浏览 1 评论 0原文

# -*- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆沫 2025-02-01 12:37:48

只需用论点调用函数全知

omniscience(1)

Just call the function omniscience with an argument

omniscience(1)
烟花易冷人易散 2025-02-01 12:37:48

简单的答案是“ Call Omniscience具有适当的参数”。在运行时,p可以是 ,因为您实际上没有使用它。如果您要执行静态类型检查,则p必须是一个函数(或更确切地说,您可以调用的内容),即使p从未被调用或使用。 (类型Checkers仅担心匹配签名,而不是运行时行为。)

让我们看一下ninf:它基本上是整数上的谓词。某些示例可以命名甚至奇数greate_than_zero等。 false基于数字的某些属性。

让我们看一下ninf_map。它是另一个谓词,但这一次是在谓词本身上。我们的抽象水平更难掌握。我们可以在谓词上定义什么样的谓词?我们不能真正“检查”谓词;我们所能做的就是在某些参数上称其为“它在每种情况下都返回的内容,

这都是一个微不足道的示例。

# satisfiable has type Ninf_map. It will return
# true if there is *some* number n for which p(n) is true.
def statisfiable(p: Ninf) -> bool:
    if p(0):
        return True
    for n in count(1):
        if p(n) or p(-n):
            return True
    return False

(请注意,令人满意的永远无法返回false;它返回true或它永远运行。但是, type 没关系,这就是我们这里关心的

。 /code>,不管omniscience是否使用它。

omniscience(satisfiable)  # This type checks; satisfiable has the correct type.

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 though p 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 named even, odd, greater_than_zero, etc. It takes a number, and returns True or False 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 case

Here's a trivial example.

# satisfiable has type Ninf_map. It will return
# true if there is *some* number n for which p(n) is true.
def statisfiable(p: Ninf) -> bool:
    if p(0):
        return True
    for n in count(1):
        if p(n) or p(-n):
            return True
    return False

(Note that satisfiable can never return False; it either returns True or it runs forever. But, the type is OK, which is all we care about here.)

Because satisfiable has type Callable[[Ninf],bool], it is a valid argument to omniscience, regardless of whether omniscience uses it or not.

omniscience(satisfiable)  # This type checks; satisfiable has the correct type.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文