This is the 14th day of my participation in Gwen Challenge
1. Parameters Overview
In the previous section, we studied functions, involving related parameters and arguments
So let's take a look at the process of parameter passing in detail in this video. Okay?
1.1 What are Parameters
Parameters are divided into parameters and arguments.
The parameter is called a formal parameter, equivalent to X, and is used as a placeholder
The argument is called the actual argument, which is passed a specific value to the function to process
- Parameter defined in parentheses when defining a function (equivalent to X, placeholder)
- It can only be used in functions
- The arguments
- The specific arguments passed in when the function is called
- The value passed to the function by parentheses after the function to be processed by the function
- Is involved in the argument relationship
- Parameters are used for function definitions and arguments are used for function calls
- The parameter is a placeholder in the function; The arguments are passed to the function for processing after the function is called
- Arguments involve parameter passing, and there are five main types of parameters
1.3 Parameter Classification
|The serial number||The name||role|
|1||Necessary parameters||Functions are passed in the correct order when they are called|
|2||Keyword parameter||The parameter values are passed in by name when a function is called|
|3||The default parameters||Specifies parameter defaults|
|4||Indefinite length parameter||Allows the parameter to be preceded by an asterisk (*). Calling functions can accept multiple values and pass them in as tuples|
|5||Reverse parameter collection||Split list/tuple/dictionary objects passed to function arguments when calling a function|
This time, first understand the parameter type, we will study in detail next time.
2. Parameter transfer memory analysis
The argument passing of a function is essentially an assignment from argument to parameter.
In Python, everything is an object. All assignment operations are "reference assignments." Therefore, arguments in Python are passed by reference, not by value.
There are two categories:
- Write to a mutable object directly to the source object itself
- Writing to an immutable object creates a new object space that is filled with new values.
All right, let's go over mutable objects and immutable objects again.
The variable object
Dictionaries, lists, collections, custom objects, etc
Numbers, strings, tuples, functions, etc
2.1 Passing mutable objects
The passing argument is a mutable object; what is actually passed is a reference to the object. in
Instead of creating a new copy of the object in the function body, you can modify the object passed directly.
Let's look at the process of passing mutable objects in detail
Def listAppEnd (b) def listAppend (b) Print ("b:",b) print("b:",id(b)) Incoming real a listappend parameter (a) print (" a: ", a) print (" a: ", id (a))Copy the code
- Define the arguments [1,2] and the listappend function object
Create variable A and ListAppend stack areas in stack memory, respectively
Create variable B in the ListAppend stack area
- Call the listAppend () function
The argument object [1,2] is passed to the listappend() parameter b.
The argument variables A and B have the same address
Since the argument a is a list data type, it is a mutable object. Mutable objects are characterized by the same address and the value can be changed.
Therefore, the reference to object 30 is still added at the end of the variable a object [1,2]
Finally, print() is called to print the results to the computer screen
2.2 Passing immutable objects
The passing argument is an immutable object (num, STR,tuple, etc.), but the actual passing is a reference to the object.
During the assignment operation, the system creates a new object because an immutable object cannot be modified.
Again, let's look at chestnuts
Def Sum(a) = 10 def Sum(b): Print ("b:",b) print("b:", id(b)) print("b:", id(b)) So will create a new address space print (" b: ", b) # b variable object is to point to the new print (" b: ", id (b)) # calling function Sum (a) print (" a: ", a) print (" a: ", id (a))Copy the code
- Define the argument as 10 and the function Sum()
The variables A and Sum are created in stack memory
Create variable B in the Sum stack area
- Call the function Sum()
Pass the object 10 of variable A to the Sum() parameter b
Since object 10 is an immutable object, it is necessary to recreate the new address if it changes
The variable b is redirected to the new address
2.3 Passing immutable Objects Includes mutable objects
With the previous chapter of shallow copy learning, understand the principle of shallow copy.
The pass argument is an immutable object, and in the write operation, a copy of the new object is created. The copying process is shallow copy (reference copy), not deep copy.
As an example
Def test(a) = (1,[3, 4]) def test(b): print("b:", id(b)) print("b:", id(b)) b = 100 print("b:",b) print("b:", id(b)) print("b:", Id # (b) ) calls the function test (a) print (" a: ", a) print (" a: ", id (a))Copy the code
- Define the variables a and the test() function
Create variable a in stack to point to heap object (1,[2,3])
Create the test stack area in the stack area, and create variable B in the test stack area
- Call a function
The variable A object is passed to the function test variable B
Variable a and variable refer to the same address (1,[2,3])
Because object A  is mutable, no new space is created
The position of a is more loosely referenced, pointing to the object 100 position
After the above introduction, we have an understanding of parameters and arguments, and parameter passing to learn deeply.
Next, we will continue to learn about parameter types and usage.
Ok, that's the content of this issue, welcome to the comments section, see you next time ~