变量引用及底层存储原理

Python中的变量


  变量(Variable)在计算机编程中于关联的标识符配对的内存存储位置,在使用时含相关类型的值,这个值可以修改。 这里的标识符就是变量的名字。

在Python当变量被使用的时候,首先在内存里将会产生两个动作

  • 第一:开辟一个指定地址的空间
  • 第二:赋予指定的变量值

在Python语言中,变量在指定的同时,必须强制赋予初始值,否则解释器报错。

Python变量值类型

  在所有编程语言中的变量值都是分类型的,Python语言的变量值的类型在赋值后才被隐性确定。例如A=0 那么0就是整数类型的值; A = “OK”,那么OK就是字符串类型的值,A = True,那么True则是波尔类型的值。
Python语言的基本变量类型包括:字符串(String)、数字(Numeric)、列表(List)、字典(Dict)、元组(Tuple)五大类。

  • name = “yankerp” ## String
  • price = 100 ## numeric
  • names = [“yankerp”, “list”, “wangwu”] ## list
  • name_data = {name1 : “yankerp”, name2 : “wangwu”} ## dict
  • name = (“yankerp”, “zhangsan”) ## tuple

引用

在Python当变量被使用的时候,首先在内存里将会产生两个动作

  • 第一:开辟一个指定地址的空间
  • 第二:赋予指定的变量值
>>> a = 100
>>> b = a
>>> c = 1314
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492206240
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492206240
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3649264
----------------------------------------------------------
A:492206240
B:492206240
C:3649264

输出结果A与B的内存地址是一个地址,当使用变量A和B时,使用的是同一个对象。
在这里插入图片描述C为新开辟的一块新的内存地址并赋予指定的变量值。

>>> a = 100
>>> b = a
>>> c = b
>>> d = c
>>> e = d
>>> f = e
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492206240
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492206240
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:492206240
>>> print(f"This is the ID of D:{id(d)}")
This is the ID of D:492206240
>>> print(f"This is the ID of E:{id(e)}")
This is the ID of E:492206240
>>> print(f"This is the ID of F:{id(f)}")
This is the ID of F:492206240
----------------------------------------------
492206240
不同变量名不同变量值
>>> a = 1000
>>> b = 2000
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:3649264
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> a = 1000
>>> b = a
>>> c = b
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:3646576
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3646576
>>> a = a + 100
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:31166256
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:3646576
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:3646576

A 为不可变类型对象,A = 1000 首先会在内存中查找有没有1000这个值的内存位置,如果没有那么将会在内存中划分一块内存空间区域,并赋予指定的变量值与变量名进行关联。 这时候1000的值对应内存地址为:31166256,当B = A时相当于B = 31166256 C = B 时相当于 C = 31166256 所以 A B C 都为同一个对象。

列表变量类型可变对象
>>> a = [1, 2, 3]
>>> b = a
>>> c = b
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:36806152
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:36806152
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:36806152
>>> a.append(4)
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:36806152
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:36806152
>>> print(f"This is the ID of C:{id(c)}")
This is the ID of C:36806152
>>> print(a)
[1, 2, 3, 4]
>>> print(b)
[1, 2, 3, 4]
>>> print(c)
[1, 2, 3, 4]

对于列表可变对象,A = [1, 2, 3]当A值修改时,系统会自动在A–>对应内存地址中扩容,所以不需要修改地址。当A append后 B 和 C 的内存地址不变,随着A的增加而增加。

不可变对象
>>> a = 1
>>> b = a
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492203072
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492203072
>>> a += 1
>>> print(f"This is the ID of A:{id(a)}")
This is the ID of A:492203104
>>> print(f"This is the ID of B:{id(b)}")
This is the ID of B:492203072
可变类型及不可变类型:

可变类型:

  • 列表:List
  • 字典:dict

不可变类型:

  • 数值类型:int, long, bool, float
  • 字符串:str
  • 元组:tuple

在这里插入图片描述在这里插入图片描述

缓存数据机制

  在文章上面提到:A 为不可变类型对象,A = 1000 首先会在内存中查找有没有1000这个值的内存位置,如果没有那么将会在内存中划分一块内存空间区域,并赋予指定的变量值与变量名进行关联。

>>> name1 = "yankerp"
>>> name2 = "yankerp"
>>> print(f"This is the ID of name1:{id(name1)}")
This is the ID of name1:38801456
>>> print(f"This is the ID of name2:{id(name2)}")
This is the ID of name2:38801456

当name1 = “Yankerp”时,内存ID:38801456 当name2 = "Yankerp"相当于:name2 = 38801456 用的为同一个地址。

>>> print(f"This is the ID of name3:{id(name3)}")
This is the ID of name3:38801456

它们都是使用的同一块地址

缓存数据机制对于可变类型
>>> name1 = [1,2,3]
>>> name2 = [1,2,3]
>>> print(f"This is the ID of name1:{id(name1)}")
This is the ID of name1:38772232
>>> print(f"This is the ID of name2:{id(name2)}")
This is the ID of name2:38784264

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39591494/article/details/100710622