1.
Python passes everything the same way, but calling it "by value" or "by reference" will not clear everything up, since Python's semantics are different than the languages for which those terms usually apply. If I was to describe it, I would say that all passing was by value, and that the value was an object reference.
Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?
It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.
Actually, what Python passes includes both arguments and return statements.
2.
Python中函数的参数传递问题,函数参数的传递往往是一个难以理解的概念,记得在C语言中有一个经典的例子如下所示:
1 int swap(int a,int b) 2 { 3 int temp; 4 temp = a; 5 a = b; 6 b = temp; 7 8 return 0; 9 } 10 11 int a = 10,b = 20; 12 printf("Before Swap a = %d, b = %d\n",a,b); 13 swap(a,b); 14 printf("After Swap a= %d,b = %d\n",a,b);
>>> IntNum = 10 >>> Num1 = IntNum >>> id(IntNum),id(Num1) (10417212, 10417212) >>> Num2 = 10 >>> id(IntNum),id(Num1),id(Num2) (10417212, 10417212, 10417212) >>> intNum = 20 >>> Num1 = 30 >>> Num2 = 40 >>> id(IntNum),id(Num1),id(Num2) (10417212, 10416972, 10416852)
#list >>> list1 = [1,2,3,4,5] >>> list2 = [1,2,3,4,6] >>> id(list1),id(list2) (19050128, 19034520) >>> list1[4]=6 >>> list1 [1, 2, 3, 4, 6] >>> id(list1),id(list2) (19050128, 19034520) #dict >>> dict1 = {'a':1,'b':2,'c':3,'d':4} >>> dict2 = {'a':1,'b':2,'c':3,'d':5} >>> id(dict1),id(dict2) (19042496, 19021232) >>> dict1['d'] = 5 >>> dict1 {'a': 1, 'c': 3, 'b': 2, 'd': 5} >>> dict2 {'a': 1, 'c': 3, 'b': 2, 'd': 5} >>> id(dict1),id(dict2) (19042496, 19021232)
def function(args):
function_block
>>> def modifier(number,string,list): number = 5 string = 'GoodBye' list = [4,5,6] print "Inside:", number,string,list >>> num = 10 >>> string = 'Hello' >>> list = [1,2,3] >>> print 'Before:', num, string, list Before: 10 Hello [1, 2, 3] >>> modifier(num,string,list) Inside: 5 GoodBye [4, 5, 6] >>> print 'After:', num, string, list After: 10 Hello [1, 2, 3]
>>> def modifier(list,dict): list[0] = 10 dict['a'] = 10 print 'Inside list = %s, dict = %s' %(list,dict) >>> dict = {'a':1,'b':2,'c':3} >>> list = [1,2,3,4,5] >>> print 'Before list = %s, dict = %s' %(list,dict) Before list = [1, 2, 3, 4, 5], dict = {'a': 1, 'c': 3, 'b': 2} >>> modifier(list,dict) Inside list = [10, 2, 3, 4, 5], dict = {'a': 10, 'c': 3, 'b': 2} >>> print 'After list = %s, dict = %s' %(list,dict) After list = [10, 2, 3, 4, 5], dict = {'a': 10, 'c': 3, 'b': 2}
>>> def swap(list): temp = list[0] list[0] = list[1] list[1] = temp >>> list = [10,20] >>> list [10, 20] >>> swap(list) >>> list [20, 10]