Python_类变量与实例变量

此页面是否是列表页或首页?未找到合适正文内容。

Python_类变量与实例变量

标签:soap内存pre变量selfnamesoapython增删改

1.实例变量:作用于实例的内部,可以在实例化之后进行增删改。

2.类变量:类中自带的变量,当实例未对类变量重新赋值就直接调用时,会直接查找到类中变量的内存地址。

测试用例如下

# Author:soap
class test:
n = 123
n_list = []
def __init__(self,name):
self.name = name

r1 = test(‘soap‘)
r2 = test(‘Dylan‘)

r1.n = ‘r1‘
print(r1.n,r2.n,test.n)

r1.n_list.append(‘from r1‘)
r2.n_list = [‘from r2‘]

print(r1.n_list,r2.n_list,test.n_list)

  

Python_类变量与实例变量

标签:soap内存pre变量selfnamesoapython增删改

原文地址:https://www.cnblogs.com/soapolddaddy/p/8979822.html

作者: 鲁大师

为您推荐

返回顶部