[英]what is the meaning of “if False: from typing import Type”?
While going over pytest src code I found that peculiar statement, can someone please explain? 在查看pytest src代码的同时我发现了一个特殊的声明,有人可以解释一下吗?
if False: # TYPE_CHECKING
from typing import Type
I'm actually looking over PEP 526 currently, and I think that this may somehow be connected to annotations, but not sure how. 我现在正在查看PEP 526 ,我认为这可能会以某种方式与注释相关联,但不确定如何。 Is it possible that someone would annotate False
in a way that it would evaluate to True
? 是否有人会以评估为True
的方式注释False
?
After a quick look in the typing src, I found that: 在快速查看输入 src后,我发现:
# Constant that's True when type checking, but False here.
TYPE_CHECKING = False
So from what I gather so far, @chepner is right. 所以从我到目前为止收集的内容来看,@ chepner是对的。 This statement is evaluated to True
when running a type check, and then the actual import is probably needed. 运行类型检查时,此语句被评估为True
,然后可能需要实际导入。 Though that still looks really weird to my eyes ( #if DBG
in C was way clearer) 虽然我的眼睛看起来仍然很奇怪(C中的#if DBG
更清晰)
if False:
...
was an old way of writing what's now (since Python 3.5.2) done with 是一种古老的写作方式(自Python 3.5.2开始)
from typing import TYPE_CHECKING
if TYPE_CHECKING:
...
TYPE_CHECKING
is TYPE_CHECKING
是
A special constant that is assumed to be
True
by 3rd party static type checkers. 由第三方静态类型检查器假定为True
的特殊常量。 It isFalse
at runtime. 它在运行时是False
的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.