I have one User defined Object. I want to pass as reference (out parameter) so that value of input Object will be changed inside function and return to called function. This can be achieved by using list or Dictionary i.e mutable object. But How to achieve with User defined object without using list & Dictionary. Check below code snippet as example:-
class Test:
def __init__(self,data):
self.data = data
def display(root): # Simple Display function
print(root.data)
#Don't want to use List to do pass by reference and out parameter. Instead pass object itself.
def assign(root,args):
if root is not None:
args[0] = root
#Passed object itself, not as List
def assign1(root,temp):
if root is not None:
temp = root
#Driver Function call
root = Test(10)
display(root)
temp = None
args = [temp]
assign(root,args) # Function in which args passed as out parameter
display(args[0]) # Gives Output: 10
temp1 = None
assign1(root,temp1) # Function in which object passed as out parameter
print(temp1) # Gives Output: None
display(temp1) # AttributeError: 'NoneType' object has no attribute 'data'
May be i am missing something.