Python basis of 02 variables

There are two variables in Python features:

1. no need to declare

a = 1

2. not tied to the type of

a = 1
a = 'hello world'

The variable name is just a reference to a specific object in memory (reference).

 

For a = 1, the memory model is as follows:

for

a = 1
b = a

Memory model is as follows:

 

Variables can be obtained by id (x) x object referenced memory address

a = 1
b = a
print('address of the object referenced by a:', id(a))
print('address of the object referenced by b:', id(a))

Output is as follows:

address of the object referenced by a: 4415551552
address of the object referenced by b: 4415551552

 

For Python in the list, each element is a list of references.

mylist = [1, 2, 3]
print('address of the object referenced by mylist:', id(mylist))
for index, item in enumerate(mylist):
    print('address of the object referenced by mylist[{}]: {}'.format(index, id(item)))

Output:

address of the object referenced by mylist: 4350869128
address of the object referenced by mylist[0]: 4345657408
address of the object referenced by mylist[1]: 4345657440
address of the object referenced by mylist[2]: 4345657472

Memory model is as follows:

 

For the parameters in Python function, only the reference to an object reference passed in a variable.

 

Reproduced in: https: //www.cnblogs.com/gattaca/p/7571563.html

Guess you like

Origin blog.csdn.net/weixin_33939380/article/details/93401912