python variable type immutable type

A variable type immutable type characteristics

1. immutable data type

Immutable data types when the first declaration of an assignment statement, will open up a space in memory, used to store the value of this variable is assigned, and this variable is actually stored, not been given this value, but store the memory address where the value of this space, you can remove the data in memory through this address variables. the so-called immutable means that we can not change the value of this data in memory, so when we change the assignment of this variable, only in memory again opened up a space for a new data store these in this a new address in memory, while the original variable that is not in the referenced memory address of the original data and references to memory addresses into the new data.

for example:

>>> x = 18
>>> id(x)
4497811200
>>> id(18)
4497811200
>>> x = 19 
>>> id(x)
4497811232
>>> id(18)
4497811200
>>> y = 18
>>> id(y)
4497811200
>>> 

 

 

 

 Start x = 18, to open up a memory address to 4497811200, or 18 corresponding to the address is 4497811200, then x = 19, to re-open an address for the memory to put 4497811232 19, we can see the same mean 18, and 19 addresses in memory will not change, will be 18 when assigned to y, y is the address pointed to 4,497,811,200.

1. Variable Data Type

Binding immutable data type, it is well understood that the type of the variable data, the variable data type is a value at the memory address pointed to by the variables can be changed.

>>> x = [1,2,3]
>>> id(x)
4501838920
>>> y = [1,2,3]
>>> z = [1,2,3]
>>> id(y)
4501838600
>>> id(z)
4501838664

From another point of view:

Variable: If the value of the variable corresponding to the data type has changed, it changes the corresponding memory address also occurs for this data type, the data type is said immutable.

Variable Data type: when the value of the variable corresponding to the data type has changed, then the corresponding memory address that does not change for this data type, to said variable data type.

#a value changed, a corresponding memory address also changes 
>>> = a. 1 >>> ID (a) 4497810656 >>> = a 2 >>> ID (2 ) 4497810688
# a direct operation, rather to a copy of a value of the operation in the other memory address, the value of a constant >>> +. 1 a . 3 >>> (a) ID 4,497,810,688 >>> a 2
value changed #b, b corresponding the same memory address, for the first time when the assignment to b, b to divide a piece of memory space, which would change the >>> b = [, 2, 3 ] >>> the above mentioned id (b) 4,501,839,496
# directly operation b, the value of b is changed, the same memory space point b >>> b.append (. 4 ) >>> ID (b) 4,501,839,496 >>> b [ . 1, 2,. 3,. 4 ] >>>

 

II. Which is a variable type which is immutable

Immutable: Number The (digital), String (String), Tuple (tuple).

#整型
a = 1
print(id(a), type(a))
a = 2
print(id(a), type(a))

# 4361254304 <class 'int'>
# 4361254336 <class 'int'>
#字符串
b = 'anne'
print(id(b),type(b))
b = 'anne1995'
print(id(b),type(b))

# 4363638744 <class 'str'>
# 4363684784 <class 'str'>

#元组
c1 = ['1','2']
c = (1,2, C1)
 Print (C, ID (C), type (C)) 
C1 [ . 1] = ' DJX ' 
Print (C, ID (C), type (C)) 

# (. 1, 2, [ '. 1', '2']) 4363948248 <class 'tuple'> 
# (. 1, 2, [ '. 1', 'DJX']) 4363948248 <class 'tuple'> 

Note: At this tuple value changed and the memory address that is not change, but we still called tuple is immutable, why? In fact, change is a list of tuples, lists are mutable type, change the value of the address remains the same. However, the definition of tuples is immutable,

the tuple is referred to as read-only list, i.e., data can be queried but can not be modified.

You can change: the Set (collection), List (list), Dictionary (dictionary).

#集合
s = {1, 'd', '34', '1', 1}
print(s, type(s), id(s))
s.add('djx')
print(s, type(s), id(s))

# {1, '1', 'd', '34'} <class 'set'> 4401385256
# {1, '1', '34', 'd', 'djx'} <class 'set'> 4401385256



#列表
list = [1,'q','qwer',True]
print(list,type(list),id(list))
list.append('djx')
print(list,type(list),id(list))

# [1, 'q', 'qwer', True] <class 'list'> 4401113608
# [1, 'q', 'qwer', True, 'djx'] <class 'list'> 4401113608

#字典
tuple = (1)
dic = {1:2}
d = { tuple:1,'key2':'djx','key3':'li'}
print(d,type(d),id(d))
d['key4'] = 'haha'
print(d,type(d),id(d))

# {1: 1, 'key2': 'djx', 'key3': 'li'} <class 'dict'> 4401075976
# {1: 1, 'key2': 'djx', 'key3': 'li', 'key4': 'haha'} <class 'dict'> 4401075976

 

Guess you like

Origin www.cnblogs.com/jiangfan95/p/11468721.html