python class 一点总结

来源:互联网 发布:网络协议详解 编辑:程序博客网 时间:2024/06/05 07:02

Python class 总结

细数class中的 __**__

  • __init__(self, *values)
    对象的初始化函数,初始化类的实例时,会调用这个方法
  • __str__(self)
    返回 print对象时要打印的东西,pirnt(obj)时会调用这个方法
  • __iter__(self)__next__(self)
    将对象 变为可迭代对象,__iter__()用于iter(),__next__用于next()
  • __getitem__(self, key)
    使得对象可以向类表一样使用,obj[2], obj[2:4]
  • __setitem__(self, key, value)
    可以使对象的key被赋值
obj['hello'] = 'world'
  • __getattr__(self, attr)
    如果对象没有所调用的属性的时候,就会把属性名送进这个方法,看看这个方法返回什么
  • __getattribute__(self, item)
    所有的属性访问都会经过这个接口
class People(object):    def __init__(self):        self.name = "h"    def __getattribute__(self, item):        print("get->",item)        return object.__getattribute__(self,item)    def __getattr__(self, item):        print("hello")    def m(self):        self.name = 'a'p = People()p.m()print(p.name)# get-> m# get-> name# a
  • __setattr__(self, name, value)
    当每次给一个属性赋值的时候,就会调用这个方法
class People(object):    def __init__(self):        self.name = "h"    def __setattr__(self, key, value):        super(People, self).__setattr__(key,value)        #self.__dict__[key] = value        print("hello")    def m(self):        self.name = 'a'p = People()p.m()print(p.name)# hello# hello# a 
  • __call__(self, *value)
    使得类的实例可以像函数一样被调用

  • __len__(self)
    当执行len(obj)时,被调用

  • __slots__
    这个和前几个不太一样,它是个类的属性,不是方法,这个用来限制类的实例所能动态添加的属性

细数用在class中的装饰器

  1. @property
    是一个装饰器,将一个方法当作属性调用
  2. @staticmethod
    将方法定义成静态方法,不需要传入self
  3. @classmethod
    将一个方法定义成类方法,传入cls而不是self
  4. @abstraction
    将一个方法声明成抽象方法

参考资料

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186739713011a09b63dcbd42cc87f907a778b3ac73000
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186781871161bc8d6497004764b398401a401d4cce000
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319098638265527beb24f7840aa97de564ccc7f20f6000
https://jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/
http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
https://docs.python.org/3/library/functions.html#setattr

0 0