标签:经典的 += type 可变 安全性 地址 没有 数据 语句
#python和java中的变量本质不一样,python的变量实质上是一个指针 int str, 便利贴 a = 1 a = "abc" #1. a贴在1上面 #2. 先生成对象 然后贴便利贴 a = [1,2,3] b = a print (id(a), id(b)) print (a is b) b.append(4) print (a) --- 1642030876232 1642030876232 True [1, 2, 3, 4]
is 判断是否是同一个对象,id是否相等
a = [1,2,3,4] b = [1,2,3,4] print(a == b) # 判断值是否相等 print (id(a), id(b)) print (a is b) c=1 d=1 print(c is d) # 将一定范围的小整数,内存地址进行了复用
# --- True 1642030933512 1642030785544 False True --- class People: pass person = People() if type(person) is People: print ("yes") type(person) --- yes __main__.People
# cpython中垃圾回收的算法是采用 引用计数 # 当计数器减为0 就会被回收 a = object() b = a del a print(b) print(a) class A: def __del__(self): # 当被回收的时候,执行下面的逻辑 pass
为了数据的安全性,使用tuple而不是list类型来存储
def add(a, b): a += b return a a = 1 b = 2 c = add(a,b) print(c) print(a,b) # --- 3 1 2 # ---传递进来的list受到了影响 a = [1,2] b = [3,4] c = add(a,b) print(c) print(a,b) # --- [1, 2, 3, 4] [1, 2, 3, 4] [3, 4] # --- a = (1,2) b = (3,4) c = add(a,b) print(c) print(a,b)
--- (1, 2, 3, 4) (1, 2) (3, 4)
---
class Company: def __init__(self, name, staffs=[]): self.name = name self.staffs = staffs def add(self, staff_name): self.staffs.append(staff_name) def remove(self, staff_name): self.staffs.remove(staff_name) com1 = Company("com1", ["lewen1", "lewen2"]) com1.add("lewen3") com1.remove("lewen1") print("com1.staffs:",com1.staffs) com2 = Company("com2") # 使用了默认的[],list又是可变的对象 com2.add("lewen") print(com2.staffs) print (Company.__init__.__defaults__) # 没有传递参数时,都使用默认的【】值 com3 = Company("com3") # 使用了默认的[],list又是可变的对象 com3.add("lewen5") print (com2.staffs) print (com3.staffs) print (com2.staffs is com3.staffs) # --- com1.staffs: [‘lewen2‘, ‘lewen3‘] [‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘] ([‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘],) [‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘] [‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘, ‘lewen‘, ‘lewen5‘] True
-
标签:经典的 += type 可变 安全性 地址 没有 数据 语句
原文地址:https://www.cnblogs.com/wenyule/p/10363581.html