In the code segment below,
class Test:
def __init__(self, val):
self.val = val
def insert(x):
if x == None:
x = Test(10)
obj = None
insert(obj)
print(obj.val)
I was expecting the output of the print statement to be 10 but am getting AttributeError: 'NoneType' object has no attribute 'val'. This is because the obj is not being modified by the function and is still None after the call to the insert function. As far as I knew, all objects are passed by reference in python. How can I get around this and modify the object without returning its new value?
xfrominsertfunction.def insert(x): if x[0] == None: x[0] = Test(10); obj=[None];insert(obj);print(obj[0].val)