Python variables, object memory addresses, and parameter passing procedures

Source: Internet
Author: User

As a novice from C/+ + turn around, just touch the variables of Python is not suitable for his behavior is like a pointer, void*, do not know that everyone has this feeling. In fact, Python is data-based, variables can be understood as tags. As a rookie of C/C + +, take the habit of tracking variable addresses into Python, give a small example of Python variables, objects, and parameter passing.

1 " " Example 1 " " 2 x = 13def fun (x):4     x = 25     return  None67Fun(x)8print(x)

Actually does not print also can, we use the Pycharm one-step debugging, looks at each line execution, the variable x value change, and its address change (actually this sentence should change: The variable x direction change is more accurate)

The official explanation for the ID () function in Python, which can return an object's address, is the "identity" of an object, since it is an identity and is certainly unique; The official said: CPython implementation Detail:this is the address of the object in memory. We temporarily consider the ID () return value as the address of the object in memory.

The first step: Enter Debug, in the Watch window, add to the ID (x), and the ID (y) of the observation, blue highlighting, indicating the next step will be executed, we see at this time, X, Y is not assigned address

Second step: To do the next, we find the variable x, start assigning the address, 1392686144, we write down this number.

Step three: Enter the function, execute the x = 2 statement, we found that the address of X has become 1392686176, this is the nature of the Python variable, we can not understand the variable x is assigned to 2, but "the name of the X tag to object 2", so more accurate.

Fourth step: Return to the fun (x) function, we found that the ID () value of x back to the original number, in this case, we put the local variables and global variables with the same label, when the function is called, when the functions inside the function, the system will create a stack, preserving the running environment and data before entering the function. After entering the function, there is a label created with the same name x,x = 2, the local label points to the local object 2, which is the local label x points to a new object, the memory address must change, when return none, return function call, stack revocation, local object, variable with the undo, local label X also revoked, At this point, X becomes the global label X, and still points to the number object 1. That's why inside the function, the label x points to the other object, returns the call, and restores the memory address before the call.

In the first example, the memory address change from Tag x helps us understand the behavior of Python's variables.

In the second example, we still understand the process of parameter passing by monitoring the memory address change of the tag.

1 " " Example 2 " " 2 a = []3def Fun (a):4     a.append (1)5      return  None67Fun(a)8Print (a)

The first step: Execute function call, parameter assignment, blue highlight is the code that will be executed next. We found that in this step, a parameter assignment has occurred, a stack is created, the memory address of the local label X is the same as the memory address of the external label A, indicating this step, the completion of the parameter assignment, can we interpret the Python "assignment statement" as "label pointing" this action? From this perspective, it is possible to understand this. So the "parameter assignment" action can be understood as a unified label pointing.

Second step: When the function returns, we find that the address of List A does not change, and the list of element 1 is preserved, not because the local variable is revoked, this time a typical through the label (reference), in the local process to change the global variable example. Is this characteristic of the Python tag much like a reference in C + +? Does it look like a pointer in C?

Summary: Python variables, we use the label to understand, reference C void*, referring to C + + &,python assignment action, can be understood as "label Change Point" action. Parameter passing process, which is the process that the interchange label points to

Python variables, object memory addresses, and parameter passing procedures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.