Click here for the management blog article Designing an excellent object storage method for python
Here, I will write about the processing of python variables as seen in the interpreter. If you want to run from a file, try googled nicely.
I think the main thing to do is "where you want to call a variable (where you're doing something like var), do'print var'", but ...
Just fill in the order "variable = object".
test
var = 9
However, "var" is depressed
test
var #9
Save variable (var)
Prepare a pointer (= work) and store the save destination of the object in a variable.
Actual value (object) assignment (9 in this case) Those who are included in the memory space of Object
Object value (9)
Type Information (that is, data type, Integer)
Reference Counter (address containing variables) "
When calling, just refer to the Object using "Pointer" Pointer is defined by "=". If there is no =, you will get a syntax error
mistake "Var1 calls var, then var calls object 9." correct "Var1 refers to the same object as var. var1 is calling object 9 directly."
First, let var1 = var. Here, the hypothesis is set as follows (it is a very science system ...)
var1**Directly**Referencing the address of an object means
var1 and var are (should) be completely independent.
Something new to var**Substitution(=)**And see.
If var is completely independent of var1, you can assign a new object to var
The value of var1 should not change
Try out.
test
var = 20
var1 = var
var = 30 #Since var1 has nothing to do with var, the value of var1 should remain the same.
var #30
var1 = 20
Since they should refer to the same value, if it is ** append ** instead of ** assignment (=) **, both values should change.
test
list1 = [1,2] #list1 [1,2]
list2 = list1 #list2 [1,2]
list1.append(3) # " = "Is not used, so a new value is added to the reference of list2
list1 #[1,2,3]
list2 #[1,2,3]
After all, unless it is an assignment, it seems that the reference destination of the variable is exactly the same. If you add or slice **, both variables (here, list1 and list2) will change. (I'm getting angry ...)
The thing is, if you add list1 = [1,2]
in the middle, ** list1 and list2 will be completely independent **, so the result should change!
test
list1 = [1,2] #list1 [1,2]
list2 = list1 #list2 [1,2]
list = [1,2] #The values are the same," = "Because it uses, a completely new object is created and it becomes independent (independent) of list2.
list1.append(3) # " = "Is not used, but the value is added to the newly created list1 object. list2 is irrelevant, so leave it as it is
list1 #[1,2,3]
list2 #[1,2]
That's why I personally fell in love with it. (If you don't understand it with your own head, you will never understand it even if you read it ...)
Recommended Posts