Is the argument in the Bullet:python function a reference?

Source: Internet
Author: User

In other languages, there is a distinction between a function's passing value and a reference. On this, there is a wide spread of the argument is that one of their differences in the phenomenon is the value of the change after the transfer, is affected by the reference, not affected is the value of the pass. In the study, also encountered this problem, online about this also has some controversy, uncompromising. But in the official document, it is explicitly said to be call by object reference. Https://docs.python.org/2/tutorial/controlflow.html#id2 the actual parameters (arguments) to a function call is Introduced in the local symbol table of the called function while it is called; Thus, arguments is passed using Call by value(Where the valueis always an object Reference, not the value of the object). [1] When a function calls another function, a new local symbol table was created for the call.

Actually, call by object reference would are a better description, since if a mutable object is passed, the caller Would see any changes the callee makes to it (items inserted into a list).
Call by object reference, then the following code snippet raises questions.
[[email protected] python] # Cat func.py def  = 2return= 1print  b[[email protected] python]#  Python func.py1[[email protected] python]#  where the value of B has not changed. 

To explain this problem, you can catch a function ID provided by Python.
ID Help on built-thefunctionIDinID(...) ID Object, (object). This was guaranteed to be unique amongsimultaneously existing objects. (Hint:it ' s The object '

Rewrite the program as follows
[[email protected] python]#Cat func.pydeffunc (a):Print "Argu A id =%d, before assignment."%ID (a) a= 2Print "Argu A id =%d, after assignment."%ID (a)returna B= 1Print "Variable B id =%d, before calling function Func."%ID (b) func (b)Print "Variable B id =%d, after calling function Func."%ID (b) [[email protected] python]#python func.pyVariable B ID =14570296, before calling function Func.argu a ID=14570296, before assignment. Argu a ID = 14570272  , after assignment. Variable B ID=14570296, after calling function Func. [[email protected] python]#

As you can see, the ID of variable B before and after the function call has not changed and the value has not changed. In the body of the function, the ID was not changed before the first assignment, but changed after the assignment. As the official document describes. The Executionof a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; Whereas variable references first look in the local symbol table, and then in the local symbol tables of enclosing functions, Then on the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a Globalstatement), although they may be referenced. In fact, the most widely circulated statement is a compound type in python, such as the following code snippet
[[email protected] python] # cat func_1.pya=[1def= 2print  a[0][[email protected] Python]#  python func_1.py2

As the footnote to the document says, if you pass a mutable object, the caller can see the change in the middle of the callee. So, the function passing in Python, as the official document says, can be called call by object reference, why there is a widespread phenomenon, which is the result of the local symbol table in the function body, is a new reference. And why there is a difference, consider that there are changes in Python (mutable) and non-change (immutable) objects

Is the argument in the Bullet:python function a reference?

Related Article

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.