Why do these two statement give different outcomes...
print(Database().id == Database().id)
(which gives False)- but it gives True this way
d1 = Database()
d2 = Database()
print(d1.id == d2.id)
here's the entire code:
import random
class Database:
initialized = False
def __init__(self):
self.id = random.randint(1, 101)
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Database, cls).__new__(cls, *args, **kwargs)
return cls._instance
print(Database().id == Database().id)
# False
d1 = Database()
d2 = Database()
print(d1.id == d2.id)
# True