Home > other >  Questions about the local function refer to external function variable
Questions about the local function refer to external function variable

Time:09-17



Output: 100
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Problem is the function of AA arg why kept function in A arg? What reason??

CodePudding user response:

 
Def A (arg) :
Def AA () :
Print (' arg - 'arg)
The return of AA
Print (' AA - 'AA)
A=a (100)
Print ((a))


Shows the result is in the loop of arg, retrun AA after the print without execution

CodePudding user response:

reference 1st floor weixin_45903952 response:
 
Def A (arg) :
Def AA () :
Print (' arg - 'arg)
The return of AA
Print (' AA - 'AA)
A=a (100)
Print ((a))


Shows the result is in the loop of arg, retrun AA after the print didn't perform

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Retrun AA after the print is not performed, of course, because the AA cited a print () function (after 'arg -' arg), has been returned,

CodePudding user response:

So, arg has not been preserved

CodePudding user response:

Upstairs, you didn't understand my meaning, function is arguably A press the parameter arg is called into the stack, return arg stack is destroyed, but the use of A local function called AA arg still has A value of 100, I just want to know how this 100, arg, why didn't be destroyed with the first end of the function call,

CodePudding user response:

Below is the function in the operation steps of the underlying
 def A (arg) : # second arg=100 
Def AA () : # looked at the third step is gone, it is the function definition does not perform step # 7 confirmed function points to a memory address function AA
Print (arg) step # 8 statement execution, the output
# print (id (AA), AA)
Return step 4 AA # returns a function AA memory address

A=a (100) the first step # call a function, passing parameters of 100 # AA memory address step 5 points to a function, namely a is also a function
# print (id) (a), a)
(a) steps # 6 calls a function # 9 - a function call is completed, as well as perform step 5 functions, mainly the function return value for the
step 8 rendering

AA local function is the function of A function body, the implementation of the second step, variable arg point to 100, the fourth step after return, A function is over, but it is the function of the body member is still there, namely variable A, AA () function is still there, A compiled language may have A function to destroy the body of the function is also destroyed, python is interpreted languages, like this, all arg,
 arg=100 
Def AA ()
Print (arg)
variables a and AA all point to the same memory address, added two new print is convenient to observe, all point to the same memory address,

  • Related