What are some interesting facts about Python that are not easily noticed?

 When it comes to Python, the first impression of most students is "simple".

However, there are also many interesting and subtle things in Python. If you don’t understand them carefully, it is easy to fall into misunderstandings during the development process, and you will be unable to extricate yourself for a long time.

Below, I will introduce a few interesting things in Python.

1. Subtle strings

>>> a = "wtf"
>>> b = "wtf"
>>> a is b
True

>>> a = "wtf!"
>>> b = "wtf!"
>>> a is b
False

>>> a, b = "wtf!", "wtf!"
>>> a is b 
True
复制代码

Do you think it's amazing?

Why add the !return is False, not add the return True?

Why does !it return when adding and placing the same row True?

  • These behaviors are due to the fact that when Cpython compiles optimizations, in some cases it will try to use an existing immutable object instead of creating a new one each time. (This behavior is called string interning)

  • After residency occurs, many variables may point to the same string object in memory. (thus saving memory)

  • In the code above, strings are implicitly interned. When implicit interning occurs is implementation-dependent. Here are a few ways to guess if strings are interned:

    All strings of length 0 and length 1 are resident.

    Strings are implemented at compile time ( 'wtf' will be resident, but  ''.join(['w', 't', 'f']) will not be resident)

    Will be resident when the string contains only letters, numbers or underscores. So   not resident 'wtf!' due to inclusion  .!

  • When  the value of a and  b is set to  on the same line "wtf!" , the Python interpreter creates a new object and then references the second variable at the same time. If you do the assignment on a different line, it won't "know" that there is already an  wtf! object (because it  "wtf!" 's not implicitly resident in the way mentioned above). It's a compiler optimization, especially for Interactive environment.

2.  The difference between isand==

>>> a = 256
>>> b = 256
>>> a is b
True

>>> a = 257
>>> b = 257
>>> a is b
False

>>> a = 257; b = 257
>>> a is b
True
复制代码
  • is 运算符检查两个运算对象是否引用自同一对象 (即, 它检查两个运算对象是否相同).

  • == 运算符比较两个运算对象的值是否相等.

  • 因此is代表引用相同,==代表值相等. 下面的例子可以很好的说明这点,

    >>> [] == [] True >>> [] is [] # 这两个空列表位于不同的内存地址. False

256 是一个已经存在的对象, 而 257 不是

当你启动Python 的时候, 数值为 -5 到 256 的对象就已经被分配好了. 这些数字因为经常被使用, 所以会被提前准备好.

Python 通过这种创建小整数池的方式来避免小整数频繁的申请和销毁内存空间.

3. is not ... 不是 is (not ...)

>>> 'something' is not None
True
>>> 'something' is (not None)
False
复制代码
  • is not 是个单独的二元运算符, 与分别使用 is 和 not 不同.
  • 如果操作符两侧的变量指向同一个对象, 则 is not 的结果为 False, 否则结果为 True.

4. 逗号

>>> def f(x, y,):
...     print(x, y)
...
>>> def g(x=4, y=5,):
...     print(x, y)
...
>>> def h(x, **kwargs,):
  File "<stdin>", line 1
    def h(x, **kwargs,):
                     ^
SyntaxError: invalid syntax
>>> def h(*args,):
  File "<stdin>", line 1
    def h(*args,):
                ^
SyntaxError: invalid syntax
复制代码
  • 在Python函数的形式参数列表中, 尾随逗号并不一定是合法的.
  • 在Python中, 参数列表部分用前置逗号定义, 部分用尾随逗号定义. 这种冲突导致逗号被夹在中间, 没有规则定义它.(译:这一句看得我也很懵逼,只能强翻了.详细解释看下面的讨论帖会一目了然.)

5. 真亦假

True = False
if True == False:
    print("I've lost faith in truth!")
复制代码

输出:

I've lost faith in truth!
复制代码
  • 最初, Python 并没有 bool 型 (人们用0表示假值, 用非零值比如1作为真值). 后来他们添加了 TrueFalse, 和 bool 型, 但是, 为了向后兼容, 他们没法把 True 和 False 设置为常量, 只是设置成了内置变量.
  • Python 3 由于不再需要向后兼容, 终于可以修复这个问题了, 所以这个例子无法在 Python 3.x 中执行!

这里只是举了几个例子,上面的例子是从Github上一个非常火热的开源项目wtfpython节选的,目前该项目已经有2.8万+star,受欢迎程度可见一斑。除了英文版,它还有中文版。 

它收集了Python中各种各样奇怪且有趣的事情,感兴趣的同学可以花时间多了解一下。


hello,大家好,我是Jackpop,硕士毕业于哈尔滨工业大学,曾在华为、阿里等大厂工作,如果你对升学、就业、技术提升等有疑惑,不妨交个朋友:

我是Jackpop,我们交个朋友吧!

Guess you like

Origin juejin.im/post/7084583402841571335