简体   繁体   English

“typed=False”是什么意思?

[英]What does “typed=False” mean?

I'm looking at the (presumably) built-in module called "typing.py", and I'm trying to understand what's going on in there.我正在查看(大概)名为“typing.py”的内置模块,我试图了解那里发生了什么。 I'm specifically looking at the code below, where we see an input argument called "typed=False".我专门查看下面的代码,在那里我们看到一个名为“typed=False”的输入参数。 What does that even mean?那有什么意思?

As I understand it, "func=None" appears to mean "no function is allowed in the inputs" (correct me if I'm wrong), where "func" refers to the object-type "function" (which presumably most programmers are familiar with because it's a basic concept).据我了解,“func=None”似乎意味着“输入中不允许使用任何函数”(如果我错了,请纠正我),其中“func”指的是对象类型的“函数”(大概是大多数程序员熟悉,因为它是一个基本概念)。 But what about "typed=False"?但是“typed=False”呢?

def _tp_cache(func=None, /, *, typed=False):
    """Internal wrapper caching __getitem__ of generic types with a fallback to
    original function for non-hashable arguments.
    """
    def decorator(func):
        cached = functools.lru_cache(typed=typed)(func)
        _cleanups.append(cached.cache_clear)

        @functools.wraps(func)
        def inner(*args, **kwds):
            try:
                return cached(*args, **kwds)
            except TypeError:
                pass  # All real errors (not unhashable args) are raised below.
            return func(*args, **kwds)
        return inner

    if func is not None:
        return decorator(func)

    return decorator

As I understand it, "func=None" appears to mean "no function is allowed in the inputs" (correct me if I'm wrong), where "func" refers to the object-type "function" (which presumably most programmers are familiar with because it's a basic concept).据我了解,“func=None”似乎意味着“输入中不允许使用任何函数”(如果我错了,请纠正我),其中“func”指的是对象类型的“函数”(大概是大多数程序员熟悉,因为它是一个基本概念)。 But what about "typed=False"?但是“typed=False”呢?

None of that is correct.这些都不对。

func is a parameter name, not a type. func是参数名称,而不是类型。 =None means that it defaults to None if no value is provided. =None表示如果没有提供值,则默认为None typed is another parameter name; typed是另一个参数名称; =False means it defaults to False . =False表示它默认为False The * and / in the parameters list indicate that func is a positional parameter (ie it's always just the first argument, the caller doesn't need to say func= ) and typed is a keyword argument (ie it must always be specified as typed=SOMETHING by the caller).参数列表中的*/表示func是一个位置参数(即它总是只是第一个参数,调用者不需要说func= )并且typed是一个关键字参数(即它必须始终指定为typed=SOMETHING调用者的东西)。

Trying to understand Python code without having a basic understanding of Python syntax is going to be extremely difficult;在没有对 Python 语法有基本了解的情况下尝试理解 Python 代码将是非常困难的; it's not something that you can guess at as you've attempted to do here, especially if you're diving straight into decorator (higher-order) functions, which are a relatively advanced feature and require that you already have a very firm grasp of how basic functions work.这不是您在这里尝试做的事情可以猜到的,尤其是当您直接深入研究装饰器(高阶)函数时,这是一个相对高级的功能,需要您已经非常牢固地掌握基本功能如何工作。

I recommend making your way through a Python tutorial, eg https://docs.python.org/3/tutorial/ .我建议您阅读 Python 教程,例如https://docs.python.org/3/tutorial/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM