mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-12-22 21:00:09 +01:00
String can be tricky: Remove outdated snippet
Related to https://github.com/satwikkansal/wtfpython/issues/120
This commit is contained in:
parent
80a969a556
commit
bf967b0332
9
README.md
vendored
9
README.md
vendored
@ -179,13 +179,12 @@ True
|
||||
>>> b = "wtf!"
|
||||
>>> a is b
|
||||
False
|
||||
|
||||
>>> a = "wtf!"; b = "wtf!";
|
||||
>>> a is b
|
||||
True
|
||||
```
|
||||
|
||||
3\.
|
||||
|
||||
**Output (< Python3.7 )**
|
||||
|
||||
```py
|
||||
>>> 'a' * 20 is 'aaaaaaaaaaaaaaaaaaaa'
|
||||
True
|
||||
@ -203,8 +202,8 @@ Makes sense, right?
|
||||
* Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned)
|
||||
* Strings that are not composed of ASCII letters, digits or underscores, are not interned. This explains why `'wtf!'` was not interned due to `!`. Cpython implementation of this rule can be found [here](https://github.com/python/cpython/blob/3.6/Objects/codeobject.c#L19)
|
||||
<img src="images/string-intern/string_intern.png" alt="">
|
||||
+ When `a` and `b` are set to `"wtf!"` in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already `wtf!` as an object (because `"wtf!"` is not implicitly interned as per the facts mentioned above). It's a compiler optimization and specifically applies to the interactive environment.
|
||||
+ Constant folding is a technique for [peephole optimization](https://en.wikipedia.org/wiki/Peephole_optimization) in Python. This means the expression `'a'*20` is replaced by `'aaaaaaaaaaaaaaaaaaaa'` during compilation to reduce few clock cycles during runtime. Constant folding only occurs for strings having length less than 20. (Why? Imagine the size of `.pyc` file generated as a result of the expression `'a'*10**10`). [Here's](https://github.com/python/cpython/blob/3.6/Python/peephole.c#L288) the implementation source for the same.
|
||||
+ Note: In Python 3.7, Constant folding was moved out from peephole optimizer to the new AST optimizer with some change in logic as well, so the third snippet doesn't work for Python 3.7. You can read more about the change [here](https://bugs.python.org/issue11549).
|
||||
|
||||
---
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user