Skip to content

Commit 72e39d3

Browse files
committed
Update example "is is not what it is"
Related to satwikkansal#100
1 parent 6164ce0 commit 72e39d3

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

README.md

+42-7
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ True
180180
>>> a is b
181181
False
182182
183-
>>> a, b = "wtf!", "wtf!"
183+
>>> a = "wtf!"; b = "wtf!";
184184
>>> a is b
185185
True
186186
```
@@ -513,10 +513,11 @@ array_4 = [400, 500, 600]
513513
514514
---
515515
516-
### ▶ `is` is not what it is!
516+
### ▶ Messing around with `is` operator^
517517
518518
The following is a very famous example present all over the internet.
519519
520+
1\.
520521
```py
521522
>>> a = 256
522523
>>> b = 256
@@ -527,6 +528,37 @@ True
527528
>>> b = 257
528529
>>> a is b
529530
False
531+
```
532+
533+
2\.
534+
535+
```py
536+
>>> a = []
537+
>>> b = []
538+
>>> a is b
539+
False
540+
541+
>>> a = tuple()
542+
>>> b = tuple()
543+
>>> a is b
544+
True
545+
```
546+
547+
3\.
548+
**Output (< Python 3.7)**
549+
```
550+
>>> a, b = 257, 257
551+
True
552+
553+
>>> a = 257; b = 257
554+
>>> a is b
555+
True
556+
```
557+
558+
**Output (Python 3.7)**
559+
```
560+
>>> a, b = 257, 257
561+
False
530562
531563
>>> a = 257; b = 257
532564
>>> a is b
@@ -541,9 +573,8 @@ True
541573
* `==` operator compares the values of both the operands and checks if they are the same.
542574
* So `is` is for reference equality and `==` is for value equality. An example to clear things up,
543575
```py
544-
>>> [] == []
545-
True
546-
>>> [] is [] # These are two empty lists at two different memory locations.
576+
>>> class A: pass
577+
>>> A() is A() # These are two empty objects at two different memory locations.
547578
False
548579
```
549580
@@ -575,8 +606,11 @@ Quoting from https://docs.python.org/3/c-api/long.html
575606
576607
Here the interpreter isn't smart enough while executing `y = 257` to recognize that we've already created an integer of the value `257,` and so it goes on to create another object in the memory.
577608
609+
Similar optimization applies to other **immutable** objects like empty tuples as well. Since lists are mutable, that's why `[] is []` will return `False` and `() is ()` will return `True`. This explains our second snippet. Let's move on to the third one,
610+
578611
**Both `a` and `b` refer to the same object when initialized with same value in the same line.**
579612
613+
**Output (< Python 3.7)**
580614
```py
581615
>>> a, b = 257, 257
582616
>>> id(a)
@@ -593,6 +627,7 @@ Here the interpreter isn't smart enough while executing `y = 257` to recognize t
593627
594628
* When a and b are set to `257` 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 `257` as an object.
595629
* It's a compiler optimization and specifically applies to the interactive environment. When you enter two lines in a live interpreter, they're compiled separately, therefore optimized separately. If you were to try this example in a `.py` file, you would not see the same behavior, because the file is compiled all at once.
630+
* Why didn't this work for Python 3.7? The abstract reason is because such compiler optimizations are implementation specific (i.e. may change with version, OS, etc). I'm still figuring out what exact implementation change cause the issue, you can check out this [issue](https://github.com/satwikkansal/wtfpython/issues/100) for updates.
596631
597632
---
598633
@@ -776,6 +811,7 @@ True
776811
'wt\\"f'
777812
778813
>>> print("\n")
814+
```
779815
780816
781817
>>> print(r"\\n")
@@ -2262,8 +2298,7 @@ Ellipsis
22622298
+ In [type hinting](https://docs.python.org/3/library/typing.html) to indicate only a part of the type (like `(Callable[..., int]` or `Tuple[str, ...]`))
22632299
+ You may also use Ellipsis as a default function argument (in the cases when you want to differentiate between the "no argument passed" and "None value passed" scenarios).
22642300
2265-
2266-
---
2301+
---
22672302
22682303
### ▶ Inpinity *
22692304

0 commit comments

Comments
 (0)