I investigated in detail about variable processing in python

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 ...

How to make variables

Just fill in the order "variable = object".

test


var = 9

How to call

However, "var" is depressed

test


var #9

What's happening behind the scenes when defining variables (maybe)

  1. Save variable (var)

  2. Prepare a pointer (= work) and store the save destination of the object in a variable.

  3. Actual value (object) assignment (9 in this case) Those who are included in the memory space of Object

  4. Object value (9)

  5. Type Information (that is, data type, Integer)

  6. Reference Counter (address containing variables) "

  7. When calling, just refer to the Object using "Pointer" Pointer is defined by "=". If there is no =, you will get a syntax error

Specific example What is happening when var1 = var

mistake "Var1 calls var, then var calls object 9." correct "Var1 refers to the same object as var. var1 is calling object 9 directly."

Confirmation ① var1 = var

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

Confirmation (2) Try to ** add ** a new value with append of list.

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 ...)

In other words

  1. Assign a value to a new variable (use =)-> Create a new object, so it becomes completely independent of each other.
  2. Do not assign a new value to a variable (= do not use)-> Since the reference destination is the same, if you change either one, both values will change

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

I investigated in detail about variable processing in python
I learned about processes in Python
File processing in Python
Text processing in Python
Queue processing in Python
About __all__ in python
I want to embed a variable in a Python string
UTF8 text processing in python
I wrote python in Japanese
Think about architecture in python
Asynchronous processing (threading) in python
Image Processing Collection in Python
I understand Python in Japanese!
What I learned in Python
Using Python mode in Processing
About "for _ in range ():" in python
A story about trying to implement a private variable in Python.
Signal processing in Python (1): Fourier transform
About dtypes in Python and Cython
About parameter processing in Flask request.args
I wrote Fizz Buzz in Python
I can't install scikit-learn in Python
I wrote the queue in Python
I tried Line notification in Python
I wrote the stack in Python
I put Python 2.7 in Sakura VPS 1GB.
I tried to implement PLSA in Python
I tried to implement permutation in Python
I made a payroll program in Python!
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I can't debug python scripts in Eclipse
I set the environment variable with Docker and displayed it in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I implemented Cousera's logistic regression in Python
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
Easy image processing in Python with Pillow
I searched for prime numbers in python
I created a password tool in Python.
Duplicate prohibition processing in GAE / Python Datastore
Status of each Python processing system in 2020
Why can't I install matplotlib in python! !!
Differences in string processing between Python, Ruby, JS, PHP (combination and variable expansion)
Asynchronous processing in Python: asyncio reverse lookup reference
I want to do Dunnett's test in Python
I implemented Robinson's Bayesian Spam Filter in python
A memo that I wrote a quicksort in Python
How to embed a variable in a python string
I was able to recurse in Python: lambda
I want to create a window in Python
I tried playing a typing game in Python
When I try matplotlib in Python, it says'cairo.Context'
View the result of geometry processing in Python
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I wrote a class in Python3 and Java
I wrote "Introduction to Effect Verification" in Python
[Python] I played with natural language processing ~ transformers ~
Think about depth-priority and width-priority searches in Python