Created
November 13, 2019 20:15
-
-
Save anthony-chaudhary/d59d01ef79c5ee93f430ed324199bc65 to your computer and use it in GitHub Desktop.
Comparing python init
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Test: | |
def __init__(self, x=[]): | |
self.x = x | |
a = Test() | |
b = Test() | |
a.x.append(1) | |
print(a == b) # False | |
# Suprised... Why would this be equal? | |
print(a.x == b.x) # True | |
class Test_Isolation: | |
def __init__(self, x=None): | |
if x is None: | |
self.x = [] | |
else: | |
self.x = x | |
a = Test_Isolation() | |
b = Test_Isolation() | |
a.x.append(1) | |
# Expected | |
print(a.x == b.x) # False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment