c网站开发案例详解,可以做装修效果图的网站有哪些,wordpress有客户端吗,24小时最新国际新闻参考链接#xff1a; Python的__name __(特殊变量)
python中的new-style class要求继承Python中的一个内建类型#xff0c;
一般继承object#xff0c;也可以继承list或者dict等其他的内建类型。 在python新式类中#xff0c;可以定义一个变量__slots__#xff0c;它的作…参考链接 Python的__name __(特殊变量)
python中的new-style class要求继承Python中的一个内建类型
一般继承object也可以继承list或者dict等其他的内建类型。 在python新式类中可以定义一个变量__slots__它的作用是阻止在实例化类时为实例分配dict 默认情况下每个类都会有一个dict,通过__dict__访问这个dict维护了这个实例的所有属性举例如下 class base(object): var9 #类变量 def __init__(self): pass bbase()
print b.__dict__
b.x2 #添加实例变量
print b.__dict__运行结果 { } {x: 2} 可见实例的dict只保持实例的变量对于类的属性是不保存的类的属性包括变量和函数。 由于每次实例化一个类都要分配一个新的dict因此存在空间的浪费因此有了__slots__。 __slots__是一个元组包括了当前能访问到的属性。 当定义了slots后slots中定义的变量变成了类的描述符相当于javac中的成员变量声明 类的实例只能拥有slots中定义的变量不能再增加新的变量。注意定义了slots后就不再有dict。如下 class base(object): __slots__(x) var8 def __init__(self): pass bbase()
b.x88 #添加实例变量
print b.x
#b.y99 #无法添加slots之外的变量 (AttributeError: base object has no attribute y)
#print b.__dict__ #定义了__slots__后就不再有__dict__ (AttributeError: base object has no attribute __dict__)运行结果 88 如果类变量与slots中的变量同名则该变量被设置为
readonly如下 class base(object): __slots__(y) y22 # y是类变量,y与__slots__中的变量同名 var11 def __init__(self): pass bbase()
print b.y
print base.y
#b.y66 #AttributeError: base object attribute y is read-only运行结果 22 22 Python是一门动态语言可以在运行过程中修改实例的属性和增删方法。一般任何类的实例包含一个字典__dict__, Python通过这个字典可以将任意属性绑定到实例上。有时候我们只想使用固定的属性而不想任意绑定属性 这时候我们可以定义一个属性名称集合只有在这个集合里的名称才可以绑定。__slots__就是完成这个功能的。 class test_slots(object): __slots__x,y def printHello(self): print hello! class test(object): def printHello(self): print hello print dir(test_slots) #可以看到test_slots类结构里面包含__slots__,x,y
print dir(test)#test类结构里包含__dict__
print **************************************
tstest_slots()
ttest()
print dir(ts) #可以看到ts实例结构里面包含__slots__,x,y不能任意绑定属性
print dir(t) #t实例结构里包含__dict__,可以任意绑定属性
print ***************************************
ts.x11 #只能绑定__slots__名称集合里的属性
t.x12 #可以任意绑定属性
print ts.x,t.x
ts.y22 #只能绑定__slots__名称集合里的属性
t.y23 #可以任意绑定属性
print ts.y,t.y
#ts.z33 #无法绑定__slots__集合之外的属性(AttributeError: test_slots object has no attribute z)
t.z34 #可以任意绑定属性
print t.z 运行结果 [__class__, __delattr__, __doc__, __format__, __getattribute__, __hash__, __init__, __module__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__,
__slots__, __str__, __subclasshook__, printHello,
x, y] [__class__, __delattr__,
__dict__, __doc__, __format__, __getattribute__, __hash__, __init__, __module__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__, __weakref__, printHello] ************************************** [__class__, __delattr__, __doc__, __format__, __getattribute__, __hash__, __init__, __module__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__,
__slots__, __str__, __subclasshook__, printHello,
x, y] [__class__, __delattr__,
__dict__, __doc__, __format__, __getattribute__, __hash__, __init__, __module__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__, __weakref__, printHello] *************************************** 11 12 22 23 34 正如上面所说的默认情况下,Python的新式类和经典类的实例都有一个
dict来存储实例的属性。这在一般情况下还不错而且非常灵活 乃至在程序中可以
随意设置新的属性。但是对一些在”编译”前就知道有几个固定属性的小class来说这个dict就有点浪费内存了。 当需要创建大量实例的时候这个问题变得尤为突出。一种解决方法是在
新式类中定义一个__slots__属性。 __slots__声明中包含若干实例变量并为每个实例预留恰好足够的空间来保存每个变量这样Python就不会再使用dict从而节省空间。 【使用memory_profiler模块memory_profiler模块是在逐行的基础上测量代码的内存使用率。尽管如此它可能使得你的代码运行的更慢。使用装饰器profile来标记哪个函数被跟踪。】 下面我们看一个例子
from memory_profiler import profile
class A(object): #没有定义__slots__属性 def __init__(self,x): self.xx profile
def main(): f[A(523825) for i in range(100000)] if __name____main__: main()运行结果如下图 第2列表示该行执行后Python解释器的内存使用情况
第3列表示该行代码执行前后的内存变化。 在没有定义__slots__属性的情况下该代码共使用了20.8MiB内存。 从结果可以看出内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB 1.05MB)
from memory_profiler import profile
class A(object):#定义了__slots__属性 __slots__(x) def __init__(self,x): self.xx profile
def main(): f[A(523825) for i in range(100000)] if __name____main__: main()运行结果如下图 可以看到在定义了__slots__属性的情况下该代码共使用了6.1MiB内存比上面的20.8MiB节省了很多内存 综上所述在确定了
类的属性固定的情况下可以
使用__slots__来优化内存。 提醒:不要贸然进行这个优化把它用在所有地方。这种做法不利于代码维护而且只有生成数以千计的实例的时候才会有明显效果。 完