属性使用示例,属性和类型的区别

来源:未知 浏览 153次 时间 2021-06-10 21:37

看python社区大妈组织的内容里边有一篇讲python内存优化的用到了__slots__。然后查了一下SEO顾问服务哪家好用到了__slots__。然后查了一下总结一下。感觉非常有用

python类在进行实例化的时候教你如何做好SEO总结一下。感觉非常有用

属性使用示例

# coding: utf-8 class A(object): x = 1 def __init__(self): self.y = 2 a = A()print a.__dict__print(a.x, a.y)a.x = 10a.y = 10print(a.x, a.y) class B(object): __slots__ = ('x', 'y') x = 1 z = 2 def __init__(self): self.y = 3 # self.m = 5 # 这个是不成功的 b = B()# print(b.__dict__)print(b.x, b.z, b.y)# b.x = 10# b.z = 10b.y = 10print(b.y) class C(object): __slots__ = ('x', 'z') x = 1 def __setattr__(self, name, val): if name in C.__slots__: object.__setattr__(self, name, val) def __getattr__(self, name): return "Value of %s" % name c = C()print(c.__dict__)print(c.x)print(c.y)# c.x = 10c.z = 10c.y = 10print(c.z, c.y)c.z = 100print(c.z)

属性使用示例

标签: nameslotsdictprint