1

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.

nandyorama
  • 17
  • 5
  • can you reformat your code?? looks little out of indent! – NoobEditor Dec 20 '17 at 06:35
  • Possible duplicate of [How do I pass a variable by reference?](https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – Priyank Mehta Dec 20 '17 at 06:46
  • You can get your answer here.. https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – Priyank Mehta Dec 20 '17 at 06:47
  • @PriyankMehta dont want to use and pass as list type..In assign(root,args) method i did using list type....but i want to change using assign1() method. – nandyorama Dec 21 '17 at 10:44

0 Answers0