Python dynamic typing

Overview

Study notes for First Python 3rd Edition

agenda

--Python without type declaration --Shared reference --Shared reference and object override --"Equivalent" and "Same"

Python dynamic typing

Python without type declaration

Creating variables

a = 3

--Variables are created as soon as the value is assigned --If an assignment is made after creation, the already assigned value will be replaced with a new one. --In this case, the variable has already been created, so it is not newly created.

Type information

--The variable itself has no type information --It is the "object" that has the type information that corresponds to the variable. --Variables only have a reference to the corresponding object (the data being assigned at that time)

Handling of variables when executing a program

--All replaced with the corresponding object --Variables that do not have a corresponding object cannot be used --Variables and objects are stored and linked in different parts of memory

WS000427.png

Garbage collection

--When a new object is assigned to a variable, the previously assigned object is destroyed and the memory area occupied by that object is released **, which is called ** garbage collection **. Call

Shared reference

a = 3
b = a

How is this

WS000428.png

In this way, ** the situation where multiple variables are references to the same object ** is called ** shared reference **.

Then add a line to the previous code

a = 3
b = a
a = 'spam'

This is like this The variable a serves as a reference to the newly created string object'spam', but the variable b remains a reference to the object 3.

WS000429.png

Shared reference and object override

A list is an array of objects in square brackets that can be overwritten.

In the example below, L2 remains a reference to [2, 3, 4]

L1 = [2, 3, 4]
L2 = L1
L1 = 24

In the following example, the value of the reference object is overwritten.

#Variable object
>>> L1 = [2, 3, 4]

#Make another reference to the same object
>>> L2 = L1

#Overwriting elements
>>> L1[0] = 24

#Changes have been made to the list corresponding to L1
>>> L1
[24, 3, 4]

#It also affects L2!
>>> L2
[24, 3, 4]

If you copy it by the following method, L2 will not be changed, and ** the two variables will indicate different memory areas **.

#Variable object
>>> L1 = [2, 3, 4]

#Make a copy of L1
>>> L2 = L1[:]

#Overwriting elements
>>> L1[0] = 24

#Changes have been made to the list corresponding to L1
>>> L1
[24, 3, 4]

#L2 does not change
>>> L2
[2, 3, 4]

"Equivalent" and "same"

>>> x = 42

#Will 42 be destroyed soon?
>>> x = 'shrubbery'

** In Python, small integers and strings with a small number of characters are cached and reused **

** There are two types of comparisons of whether objects are "same" **

-== The operator compares whether an object is "equivalent" --The is operator is for comparing whether objects are "identical" -** The conditions for being "identical" are stricter than "equivalent" **

>>> L = [1, 2, 3]

#M and L are references to the same object
>>> M = L

#Comparison of whether both are "equivalent"
>>> L == M
True

#Comparison of whether they are "identical"
>>> L is M
True
>>> L = [1, 2, 3]

#The objects that M and L correspond to are different
>>> M = [1, 2, 3]

#Both values are the same
>>> L == M
True

#Two objects are equivalent but not identical
>>> L is M
False

** Doing the same for smaller numbers may result in different results **

--In the following cases, X and Y are equivalent but not the same --But small numbers and strings are cached and reused, so they both serve as references for the same object.

>>> X = 42

#The two 42s should not be the same
>>> Y = 42
>>> X == Y
True

#The two will be the same because they are cached!
>>> X is Y
True

Recommended Posts

Python dynamic typing
Python typing
[Python] Dynamic programming ABC015D
Python
Python Selenium Dynamic download wait
[Python] Dynamic programming TDPC A
Dynamic proxy with python, ruby, PHP
Typing automation notes by Python beginners
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.