Skip to content

Instantly share code, notes, and snippets.

@anthony-chaudhary
Created November 13, 2019 20:15
Show Gist options
  • Save anthony-chaudhary/d59d01ef79c5ee93f430ed324199bc65 to your computer and use it in GitHub Desktop.
Save anthony-chaudhary/d59d01ef79c5ee93f430ed324199bc65 to your computer and use it in GitHub Desktop.
Comparing python init
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