Home > other >  How to understand this simple python variable problem
How to understand this simple python variable problem

Time:10-12

Def func (ls=[]) :
Ls. Append (1)
Return the ls
A=func ()
# # # print (a) [1]
B=func ()
Print (a, b) # # # [1, 1] [1, 1]

CodePudding user response:

Very interesting question, I studied a little bit about the, feeling associated with Python's memory management, first look at this piece of code:
 
> Id ([])
2785606466440
> L=[]
> Id (l)
2785606466440
> L.A. ppend (2)
> Id (l)
2785606466440
> Id ([])
2785609579656


Note [] to create a new, empty list, when carries on the operation (such as append), its place in the memory and didn't change (at least when insert the amount is very small change), conversely we define the functions of head,
 def func (ls=[]) 

Remember Python objects are essentially reference (similar to the smart Pointers), the function head and said, what ls variable in a function reference list position is given by the user, if it is not given, the default points to the current [] the memory location,

When was the current location of the [] to determine? In each time performs this function call (such as a=func () or b=func ()), respectively, to determine when? If so, there will be no such a wonderful work as a result, in fact, the default value of the ls should be in the first run the. Py files, the Python interpreter when the full scan, a bit like C language compile time determine the macro name, we assume that the code just run into the function defined in this paragraph, [] corresponding memory address is 2785606466440, this is the interpreter will remember, ls default point to 2785606466440 stored in the memory of the list object, after the operation, that is, to the operation of the this object, remember, this is a function in Python running define certain part of the code, rather than running a function call code when certain, when running the next row
 a=func () 
, Python found that users do not specify the ls parameters, therefore, in accordance with the above logic, ls point to 2785606466440 memory, and append to the list object operation, also, execution to
 b=func () 
, ls were pointed to the 2785606466440 list object, that object a, at the back of the results would be easy to understand,

In order to verify the guess, we can add a line of print instruction in func function displays the id of the object the ls (by the way, we usually use the retaining of id function to realize it's return to the object's memory address),

 
Def func (ls=[]) :
Print (" id="(ls), id # (ls) shows the ls variable memory address
Ls. Append (1)
Return the ls

To do the above test, should be clear,
On the other hand, I agree, this reveals the Python code is very straightforward to explain logic it is counterintuitive, said it is a bug Python might be a little too much, but really hard to get people to accept.

CodePudding user response:

Under the simple said you write the code above:
: 1, the func function func function needs a parameter, the ls list, the default value is an empty list, if not provide ls parameters and the system is not the ls list, then create a ls empty list, and then add elements to the ls list 1, and then return the ls list,
2, a=func () : the first call func function, because the system failure to the ls list, so they will use the default values to create an empty list of ls, when call func function, ls from [] to [1], and then put the result directly assigned to a variable a, so a is [1],
3, b=func () : the second call func function, the system already has one in the ls list, its value is [1], when once again call func function, ls from [1] to [1, 1], and then send the results directly assigned to a variable b, so b is (1, 1),
4, why this time is a [1, 1], this is because the assignment in python is only reference, in this example, the variables a and b are pointing to the ls list, their value is derived from the ls list, when change the ls list, the value of the variable a also produces change, therefore is a [1, 1], I guess you don't understand the point here, I suggest you to learn the python direct assignment, shallow and deep copy,

CodePudding user response:

This is a very typical error model, a lot of data have to speak

CodePudding user response:

Is the data type of the variable scope is global

CodePudding user response:

Is variable, the scope of the function arguments, preach value addressed in shallow deep copy, in Python, a list of tuples dictionaries are belong to the object, function, use pass the address of the object to the parameters,
  • Related