[Tutor] passing by reference

Vineeth Mohan vineethrakesh at gmail.com
Sun Mar 13 08:58:55 CET 2011


How are variables in python passed. By value or by referrence?  For  
example if we consider a simple function below wherein the value of a 
does not get modified in the main function.

def addition(a,b):
     a = a+1
     return a+b

if __name__ == '__main__':

     a = 10
     b = 15

Is the following the only way to pass by reference? or is there any 
other way

def addition(x):
     x.a = x.a+1
     return x.a+x.b

class variables(object):
     a = 10
     b = 15

if __name__ == '__main__':

     obj = variables()
     print obj.a

More information about the Tutor mailing list