python - 当省略 if 后的空格时,为什么 python3 内联 if 语句不会引发 SyntaxError?
问题描述
Python 3.6 和最高版本 3.8 中的以下语句不会引发 SyntaxError,Python 2.7 和 Python 3.5 会这样做。
>>> ifNone: print(5)
5
>>> ifTrue: print('a')
a
>>> ifnotNone: print('foo')
foo
另请注意,在执行 print 语句之前不显示省略号。通常内联 if 语句具有以下结构。
>>> if True: print('bar')
...
bar
>>> if False: print(10)
...
>>>
什么允许这种行为?
解决方案
第一个示例可能是声明一个名为 ifNone 的变量为 print(5) 类型。
回想一下 PEP484 和 PEP526 类型注释只是对 Python 解释器的注释;你需要像 mypy 这样的东西来理解它们。
推荐阅读
- javascript - JavaScript 减少三元运算符
- vue.js - 数组内对象的VueJS反应性问题
- reactjs - 在 Typescript 中扩展和使用泛型类
- flutter - 未定义的颤振创建--ios-language objc--android-language java
- javascript - 使用 Active Directory 的 Spring Web App 的单点登录实现
- docker-compose - 将 OpenLDAP 作为 Wso2is 5.8.0 的 PRIMARY 外部用户存储设置的问题:添加新用户时出现与 createTimestamp 属性相关的“错误 21”
- apache - 如何将http重定向到https而不在apache中使用斜杠?
- google-coral - 如何在 Coral CPU 上运行 tflite 模型
- angular - 如何从服务中获取 url 的参数
- r - 如何在循环中使用 if else 语句?