Got this exercise on a python exam.
Trying to return a deep o copy of a list like this:
l = list()
l = [0,1,2]
l1 = l
l[0] = 1
l1 should contain [0,1,2] not [1,1,2]
The exercise specified to implement it by using a metaclass.
class deep(type):
def __new__(meta, classname, bases, classDict):
return type.__new__(meta, classname, bases, classDict)
def __init__(cls, name, bases, dct):
super(deep, cls).__init__(name, bases, dct)
def __call__(cls, *args, **kwds):
return type.__call__(cls, *args, **kwds)
class list(metaclass=deep):
def __init__(self):
pass
From what i know , the '=' in python is a statement and not an operator and it can’t be overriden. Any idea on how to return a deep copy on assignment? Have been trying quite a lot but with no success.
As far as I’m aware, this is not possible in python without using some kind of extra syntax. As you said, you can’t override
=.