You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-7
Original file line number
Diff line number
Diff line change
@@ -180,7 +180,7 @@ True
180
180
>>> a is b
181
181
False
182
182
183
-
>>> a, b="wtf!", "wtf!"
183
+
>>>a="wtf!"; b="wtf!";
184
184
>>> a is b
185
185
True
186
186
```
@@ -513,10 +513,11 @@ array_4 = [400, 500, 600]
513
513
514
514
---
515
515
516
-
### ▶ `is` is not what it is!
516
+
### ▶ Messing around with `is` operator^
517
517
518
518
The following is a very famous example present all over the internet.
519
519
520
+
1\.
520
521
```py
521
522
>>>a=256
522
523
>>>b=256
@@ -527,6 +528,37 @@ True
527
528
>>>b=257
528
529
>>> a is b
529
530
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
530
562
531
563
>>>a=257; b=257
532
564
>>> a is b
@@ -541,9 +573,8 @@ True
541
573
*`==` operator compares the values of both the operands and checks if they are the same.
542
574
* So `is`isfor reference equality and`==`isfor value equality. An example to clear things up,
543
575
```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.
547
578
False
548
579
```
549
580
@@ -575,8 +606,11 @@ Quoting from https://docs.python.org/3/c-api/long.html
575
606
576
607
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 objectin the memory.
577
608
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
+
578
611
**Both `a`and`b` refer to the same object when initialized with same value in the same line.**
579
612
613
+
**Output (< Python 3.7)**
580
614
```py
581
615
>>> a, b = 257, 257
582
616
>>>id(a)
@@ -593,6 +627,7 @@ Here the interpreter isn't smart enough while executing `y = 257` to recognize t
593
627
594
628
* 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.
595
629
* 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 fileis 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.
596
631
597
632
---
598
633
@@ -776,6 +811,7 @@ True
776
811
'wt\\"f'
777
812
778
813
>>>print("\n")
814
+
```
779
815
780
816
781
817
>>>print(r"\\n")
@@ -2262,8 +2298,7 @@ Ellipsis
2262
2298
+ 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, ...]`))
2263
2299
+ 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).
0 commit comments