This is about
How Python parameters are passedOf the article, mainly introduced the
python parametersVariable parameter definitions and their
python parameter passing method,Share to everyone for your reference. The specific analysis is as follows:
The so-called python parameters
In the python parameter , the type belongs to the object, and the variable is of no type:
a=[1,2,3] a= "PHPCN"
In the above code, [All-in-one] is a list type, "PHPCN" is a string type, and variable A is no type, she is simply a reference to an object (a pointer), either a list type object, or a string type object.
⊙ can Change (mutable) and non-change (immutable) objects
In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.
· Immutable type: Variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, then a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.
· Variable type: variable assignment la=[1,2,3,4] After assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.
How Python parameters are passed :
· Immutable types: Values such as C + + are passed, such as integers, strings, and tuples. such as fun (a), passing only a value, does not affect the A object itself. For example, in Fun (a) to modify the value of a, just modify another copy of the object, does not affect the a itself.
· mutable types: References such as C + + are passed, such as lists, dictionaries. such as Fun (LA), is the real transfer of LA, the modified fun outside of LA will also be affected
Everything in Python is an object, strictly meaning we can't say value passing or reference passing, we should say immutable objects and pass mutable objects.
⊙python instances of immutable objects
#!/usr/bin/python#-*-coding:utf-8-*-def changeint (a): a = ten B = 2ChangeInt (b) Print B # result is 2
The instance has int object 2, the variable that points to it is B, and when passed to the Changeint function, the variable b,a and B both point to the same int object, and at a=10, the new int value Object 10, and a points to it.
⊙ Transfer Variable Object instance
#!/usr/bin/python #-*-Coding:utf-8-*-# writable function Description def changeme (mylist): "Modify incoming list" mylist.append ([1,2,3,4]);
print "function inside Value:", MyList return # call changeme function mylist = [10,20,30]; Changeme (MyList); Print "function outside value:", MyList
The same reference is used for the object passing in the function in the instance and adding new content at the end, so the output is as follows:
Values in function: [10, 20, 30, [1, 2, 3, 4]] values outside the function: [10, 20, 30, [1, 2, 3, 4]]
After class extension:
Read through the Python custom function and the Python function return value, with a detailed example