In Python, None, numbers, functions, classes, methods, modules, etc. ** all ** are ** converted ** to ** objects ** (instances of some class).
The ** variable ** holds the ** object id **, which is the ** reference value ** to the object **. You can ** assign ** to any object. You can also ** reassign ** objects of a different type.
** Variables ** are stored in the ** Variable Dictionary ** with ** variable name ** as the dictionary key and ** object id ** as the dictionary value. You can check the contents of the ** variable dictionary ** with the vars
function, locals
function, and globals
function.
The ** list ** is a ** array ** of ** object id **. You can also assign / reassign objects of different types for each array element.
>>> id(1)
15812500000
>>> type(1)
<class'int'>
>>> 1 .__class__ #Insert a space before the dot so that it is not interpreted as a decimal point
<class'int'>
>>> 1 .real
1
>>> id(2)
15812500032
>>> a = 1
>>> id(a)
15812500000
>>> a = 2
>>> id(a)
15812500032
>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class'_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>,
'a': 2}
Assignments such as + =
are called cumulative assignments.
Cumulative assignment in Python behaves differently depending on whether the object assigned to the variable is an immutable (immutable) object or a mutable (variable) object.
For immutable objects such as int type, reassign another object as shown in the figure below.
>>> a = 2
>>> id(a)
15812500032
>>> a += 1
>>> id(a)
15812500064
>>> id(1)
15812500000
>>> id(2)
15812500032
>>> id(3)
15812500064
>>> a = [1, 2, 3]
>>> id(a)
123145300973832
>>> type(a)
<class'list'>
>>> a.__class__
<class'list'>
>>> len(a)
3
>>> a.__len__()
3
>>> for ele in a:
... print(ele, type(ele), id(ele))
...
1 <class'int'> 15812500000
2 <class'int'> 15812500032
3 <class'int'> 15812500064
>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class'_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>,
'a': [1, 2, 3],
'ele': 3}
>>> a = [1, 2, 3]
>>> id(a[0])
15812500000
>>> a[0] += 1
>>> id(a[0])
15812500032
>>> id(a[1])
15812500032
Note that integer values (int type objects) from -5
to 256
are cached as frequently used values and are ** shared and reused **, so ʻa [0]and ʻa [1]
The object ids are the same.
Object-oriented Design Patterns (https://en.wikipedia.org/wiki/%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83%91 % E3% 82% BF% E3% 83% BC% E3% 83% B3_ (% E3% 82% BD% E3% 83% 95% E3% 83% 88% E3% 82% A6% E3% 82% A7% E3% 82% A2)) "[flyweight pattern](https://ja.wikipedia.org/wiki/Flyweight_%E3%83%91%E3%82%BF%E3%83%BC%E3%83" % B3) ”is used.
Since the list is a mutable object, it behaves differently than an int type object. As shown in the figure below, the object is requested to process, and the object itself changes the object contents.
>>> a = [1, 2, 3]
>>> id(a)
123145300973832
>>> hasattr(a, '__iadd__')
True
>>> hasattr(2, '__iadd__')
False
>>> a += [4]
>>> id(a)
123145300973832
>>> len(a)
4
>>> a.__len__()
4
>>> id(a[3])
15812500096
Cumulative assignment statement: https://docs.python.org/ja/3/reference/simple_stmts.html#augmented-assignment-statements Cumulative assignment method: https://docs.python.org/ja/3/reference/datamodel.html#object.iadd
Recommended Posts