你看不懂的 Python 写法

date
May 10, 2021
slug
strange-python-code.html
status
Published
tags
python
tech
fun
summary
小朋友,你是否有很多问号
type
Post
近来在网上冲浪时看到了一个奇怪的 Python 语句:
>>> [0xfor x in (15, 10, 5)]
[15]
有那么一瞬间,对自己多年的 Python 语法产生了怀疑, 0xfor 是一个什么新的循环关键字?
经过一番探究,发现一个奇怪的 Python 语法世界。
 
上面这一句,实际上是 [0xf or x in (15, 10, 5)]
首先, 0xf 代表了 十六进制整型 15 ,同理还可以用二进制、八进制
>>> [0b010or x in (15, 10, 5)]
[2]

>>> [0o0or x in (15, 10, 5)]
[8]
还有一个特殊数字表示—— j ,代表 complex(复数)类型j 在这里表示虚数 i不用 i 的原因),也可以有很奇怪的写法:
>>> 0jor.0j
0j

>>> 0jis.0j
True
 
其次,在 Python 中操作符(Operator)左右的空格并不全是必须的
" Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token"
所以理论上你可以写出类似这样的代码
>>> 0xfin[15]
True

>>> 0o7is[0xf]
False

>>> 0o7&0xf
7

>>> ~0o0**0o0
-2
 
最后,由于 or 操作符遵循最短求值原则,只要左侧表达式为真,右侧表达式是不会执行的,所以任何奇怪的表达都可以正常运行:
>>> 0xdeadbeefor whysoserious
3735928559

>>> 0xdeadbeefor whysoserious
3735928559

>>> 0o0and whysoserious
0
 
所以综上,我们来一个终极“乱码”:
>>> 0b010&0o0&0b0is(0j)in[0xdeadbeefor["是不是很奇怪这样也能运行"]]
False
 
现在再看一眼开头, [0xfor x in (15, 10, 5)] 是不是一眼就能能把 [15] 给看出来了呢?🥴
 
参考:
  1. https://stackoverflow.com/questions/67083039/why-does-python-return-15-for-0xfor-x-in-1-2-3
  1. https://twitter.com/nedbat/status/1382027078816653319
  1. https://stackoverflow.com/questions/24812444/why-are-complex-numbers-in-python-denoted-with-j-instead-of-i
  1. https://stackoverflow.com/questions/8370637/complex-numbers-in-python
  1. https://docs.python.org/3/reference/expressions.html#operator-precedence
  1. https://docs.python.org/3/reference/lexical_analysis.html#whitespace-between-tokens
 

© bluesyu 2019 - 2023

powered by nobelium