I want when a object is referenced by another, the refernecer's self object or one of it's attributes to be different at the referencer object
This is what I want to do:
class MyClass:
.
.
.
.
a = MyClass()
b = a
print(b is a) #must print false
print(b == a) #must print true
#or
a = MyClass()
b = a
print(b.attr is a.attr) #must print false
print(b.attr == a.attr) #must print true
How can I achieve this, normally the when an assignment is made like a = b
, b
is a reference to a
, any help would be appreciated, I want b
to be a copy/deepcopy of a
, same for the attribute
Thanks from now for the people who will answer the question
Note: I’m using CPython (the official implemention of Python) version Python 3.8
I'm open for using dark magic
You shouldn't try to do anything with overloading assignment as that's not very pythonic. Either use the
deepcopy
function or make a copy "constructor". Then override the__eq__
function so that the two variables test equal.