Python全栈开发——面向对象进阶(一切皆对象)

1.isinstance(obj,cls)            检查obj是否是类cls的对象
issubclass(sub,super)          检查sub是否是super的子类

#isinstance(obj,cls)  检查obj是否是类cls的对象
class Foo:
    pass
f=Foo()
print(isinstance(f,Foo))    #True

#issubclass(sub,super)  检查sub是否是super的子类
class Bar(Foo):
    pass
print(issubclass(Bar,Foo))   #Ture

2.反射(常用于可插拔方式)

3.__setattr__,__getattr__,__delattr__

4.二次加工标准类型(包装)

5.__getattribute__

class Foo:
    def __init__(self,y):
        self.y=y
    #有__getattribute__,则__getattr__不会触发
    #实际上只有触发AttributeError时,才会触发
    def __getattr__(self, item):
        print('getattr')

    #实例化访问时,不管存不存在属性,都会调用
    def __getattribute__(self, item):
        print('getattribute')
f=Foo(3)
f.c       #  getattribute
f.y       #  getattribute

6.描述符(__get__,__set__,__delete__)

7.再看property

8.__setitem__,__getitem__,__delitem__

#通过字典形式访问才会触发,和内置attr差不多
class Foo:
    def __getitem__(self, item):
        print('getitem')

    def __setitem__(self, key, value):
        print('setitem')
        self.__dict__[key]=value

    def __delitem__(self, key):
        print('delitem')

f=Foo()
f['l']='k'    #setitem
print(f.__dict__)  #{'l': 'k'}
f['l']        #getitem
del f['ff']   #delitem

9.__str__,__repr__,__format__  

#__repr__,__str__ 控制对象的显示方式,一定有返回值,且为字符串形式 
# print的原理就是输出___str__的信息,若找不到,用__repr__代替
class Foo:
    def __str__(self):
        return 'str'

    def __repr__(self):
        return 'repr'

x=Foo()
print(x)    #str

#__format__

x='{0}-{0}-{0}'.format('d')
print(x)   #d-d-d

Date_geshi={
    'ymd':'{0.year}-{0.month}-{0.day}',
    'ydm':'{0.year}-{0.day}-{0.month}',
    'mdy':'{0.month}-{0.day}-{0.year}',
}
class Date:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
    #定制格式
    def __format__(self, format_spec='ymd'):
        if format_spec not in Date_geshi:
            print('没有该格式')
            format_spec = 'ymd'
        print(format_spec)   #  ymd
        return Date_geshi[format_spec].format(self)

d=Date(2016,12,23)
d1=d.__format__('hgfh')
print(d1)    #  2016-12-23
print(format(d,'mdy'))   #相当于执行  d.__format__('mdy')
#  12-23-2016

10.__slots__(如果一个属性很少的类,但是有很多 实例,为了节省内存,可以使用__slots__取代实例的__dict__)

class Foo: 
    #设置之后,class没有__dict__属性了
    #也限制了创建,只能定义你定义的属性
    #只能定义__slots__提供的name,age
    __slots__ = ['name','age']

f=Foo()
f.name=13

11.__doc__(打印类文档)

12.__module__    表示当前操作的对象在哪个模块

 __class__           表示当前操作的对像是哪个类   

13.__del__析构方法

14__call__对象后面加括号,触发执行

class Foo:
    def __call__(self,*args,**kwargs):
        print('__call__')

f=Foo()
f()     #__call__

15__next__,__iter__(迭代器协议) 

class FBIF:
    def __init__(self,_a,_b):
        self.__a=_a
        self.__b=_b
    def __iter__(self):
        return self
    def __next__(self):
        self.__a,self.__b=self.__b,self.__b+self.__a
        return self.__a
f=FBIF(1,1)
print(next(f))
print(next(f))
print('---------------->')
for i in f:
    print(i)
    if i>100:
        raise StopIteration('ffff')

 16.__enter__,__exit__(被称为上下文管理协议)
with 的工作原理,实际上先调用__enter__,在结束时调用__exit__扫尾

原文地址:https://www.cnblogs.com/lujiacheng-Python/p/9739314.html