zoukankan      html  css  js  c++  java
  • Python之路【第六篇】python基础 之面向对象进阶

    一 isinstance(obj,cls)和issubclass(sub,super)

    isinstance(obj,cls)检查是否obj是否是类 cls 的对象  和  issubclass(sub, super)检查sub类是否是 super 类的派生类

    1 class A:
    2     pass
    3 
    4 class B(A):
    5     pass
    6 
    7 print(issubclass(B,A)) # B是A的子类 ,返回True
    8 a1 = A()
    9 print(isinstance(a1,A))  # a1 是A 的实例

    二 反射

    1 什么是反射

    反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力(自省)。这一概念的提出很快引发了计算机科学领域关于应用反射性的研究。它首先被程序语言的设计领域所采用,并在Lisp和面向对象方面取得了成绩。

    2 python面向对象中的反射:通过字符串的形式操作对象相关的属性。python中的一切事物都是对象(都可以使用反射)

    四个可以实现自省的函数

     1 class People:
     2     country = "Chain"
     3     def __init__(self,name,age):
     4         self.name = name
     5         self.age = age
     6     def play_basketball(self):
     7         print("%s doing play basketball"%self.name)
     8     def eat(self):
     9         print("%s doing eat mice" %self.name)
    10 p1 = People("xiaoming","18")
    11 
    12 #    #################### hasattr ##############
    13 # print(hasattr(p1,"name"))  # True 查看是否存在这个方法和属性
    14 # print(hasattr(p1,"eat"))
    15 # print(hasattr(p1,"eat1")) #False
    16 
    17 #    #################### getattr ##############
    18 # print(getattr(p1,"name"))  # xiaoming  获取属性
    19 # print(getattr(p1,"eat"))  # <bound method People.eat of <__main__.People object at 0x0000027CAF6645C0>> 加括号执行
    20 # func = getattr(p1,"play_basketball")
    21 # func()
    22 
    23 #    #################### setattr 设置属性##############
    24 setattr(p1,"gender","nan")  # 为实例添加属性
    25 print(p1.__dict__)
    26 setattr(p1,"show_name",lambda self:self.name)
    27 print(p1.show_name(p1))
    28 #    #################### delattr 删除属性 ##############
    29 
    30 delattr(p1,"gender")
    31 print(p1.__dict__)
    32 delattr(p1,"show_name")
    33 print(p1.__dict__)
    34 # delattr(p1,"gender")  # 不存在则报错

    3 为什么用反射之反射的好处

    好处一:可以事先定义好接口,接口只有在被完成后才会真正执行,这实现了即插即用,这其实是一种‘后期绑定’,什么意思?即你可以事先把主要的逻辑写好(只定义接口),然后后期再去实现接口的功能

    1 class FtpClient:
    2     'ftp客户端,但是还么有实现具体的功能'
    3     def __init__(self,addr):
    4         print('正在连接服务器[%s]' %addr)
    5         self.addr=addr
    #from module import FtpClient
    f1=FtpClient('192.168.1.1')
    if hasattr(f1,'get'):
        func_get=getattr(f1,'get')
        func_get()
    else:
        print('---->不存在此方法')
        print('处理其他的逻辑')
    
    不影响代码编写

    好处二:动态导入模块(基于反射当前模块成员)

     1 # ############ 导入第一种方法 ########
     2 # from m1.t import *
     3 # text1()
     4 # # _text2() # 无法调用
     5 
     6 # ############ 导入第二种方法 ########
     7 # module_t = __import__("m1.t")
     8 # print(module_t)  # <module 'm1' (namespace)>
     9 # module_t.t.text1()
    10 
    11 # ############ 导入第三种方法 ########
    12 # import importlib
    13 # m = importlib.import_module("m1.t")
    14 # print(m) # <module 'm1.t' from 'C:\Users\18368\PycharmProjects\project\week3\day1-object-oriented\m1\t.py'>
    15 # m.text1()
    16 # m._text2()
    三种导入模式

    反射总结:

    hasattr(obj,'属性') #obj.属性 是否存在
    getattr(obj,'属性') #获取obj.属性 不存在则报错
    getattr(obj,'属性','默认值') #获取obj.属性 不存在不会报错,返回那个默认值
    setattr(obj,'属性','属性的值') #obj.属性=属性的值
    delattr(obj,'属性') #del obj.属性

    三 __setattr__,__delattr__,__getattr__

    obj点的方式去操作属性时触发的方法

    三个参数,给对象添加属性

    __setattr__   添加/修改属性会触发它的执行

    __delattr__   删除属性的时候会触发

    __getattr__  重点: 只有在使用点调用属性且属性不存在的时候才会触发

    作用:系统内置函数属性(你定义了就用你定义的函数属性,不定义就用系统默认的函数属性)

     1 # 1、__getattr__  当没有内置属性,方法的时候会触发__getattr__
     2 class Foo:
     3     x = 1
     4     def __init__(self,y):
     5         self.y =y
     6     def __getattr__(self, item):
     7         print("get")
     8 f1 = Foo(2)
     9 print(f1.y)  # 2
    10 print(getattr(f1,"y"))  #  相当于str ---> str.__len__()
    11 f1.ss   #  do  当没有内置属性,方法的时候会触发__getattr__
    12 
    13 # 2   delete  直接触发 __delattr__
    14 class Foo:
    15     x = 1
    16     def __init__(self,y):
    17         self.y =y
    18     def __delattr__(self, item):
    19         print("delete")
    20 f1 = Foo(2)
    21 print(f1.y)  # 2
    22 
    23 del f1.y  # delete  直接触发 __delattr__
    24 del f1.x  # delete
    25 
    26 # 3  添加/修改属性会触发__setattr__的执行
    27 class Foo: 
    28     x = 1
    29     def __init__(self,y):
    30         self.y =y
    31     def __setattr__(self, key, value):
    32         print("set")
    33         # self.key = value    会陷入无限的递归
    34         self.__dict__[key] = value
    35 f1 = Foo(2)
    36 print(f1.__dict__)
    37 f1.z = 2
    38 print(f1.__dict__)
    三种内置方法

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

    包装:python为大家提供了标准数据类型,以及丰富的内置方法,其实在很多场景下我们都需要基于标准数据类型来定制我们自己的数据类型,新增/改写方法,这就用到了我们刚学的继承/派生知识(其他的标准类型均可以通过下面的方式进行二次加工)

     1 class List(list):
     2     def append(self, p_object):
     3         '我改写的方法'
     4         if not isinstance(p_object,str):
     5             print('只有字符串类型能被添加到列表中')
     6             return
     7         # self.append(p_object) #进入无限递归
     8         super().append(p_object)
     9     def show_mid(self):
    10         '我新增的方法'
    11         index=int(len(self)/2)
    12         print(self[index])
    13 
    14 
    15 l1=List('hello')
    16 
    17 l1.append('abc')
    18 print(l1,type(l1))
    19 
    20 #数字无法添加成功
    21 l1.append(1)
    22 print('-->',l1)
    23 
    24 #基于标准类型新增了功能
    25 l1.show_mid()
    二次加工标准类型

    授权:授权是包装的一个特性, 包装一个类型通常是对已存在的类型的一些定制,这种做法可以新建,修改或删除原有产品的功能。其它的则保持原样。授权的过程,即是所有更新的功能都是由新类的某部分来处理,但已存在的功能就授权给对象的默认属性。

    实现授权的关键点就是覆盖__getattr__方法

     1 import time
     2 class Filehadle:
     3     def __init__(self,filename,mode = "r",encoding = "utf-8" ):
     4         self.file = open(filename,mode,encoding=encoding)
     5         self.mode = mode
     6         self.encoding = encoding
     7     def write(self,line):
     8         print("-------",line)
     9         t = time.strftime("%Y-%m-%d %X")
    10         self.file.write("%s %s" %(t,line))
    11     def __getattr__(self, item):
    12         print("触发getattr")
    13         print(item,type(item))
    14         # self.file.read
    15         return getattr(self.file,item)
    16 f1 = Filehadle("b.txt","w+")
    17 f1.write("hello
    ")
    18 # f1.write("hello world")
    19 f1.seek(0)
    20 print(f1.read())   # 触发__getattr__ 没有的时候触发
    21 f1.close()
    授权示范

    五 __setitem__,__getitem,__delitem__

    #__getitem__,__setitem_,__delitem__
    obj[‘属性’]的方式去操作属性时触发的方法
    __getitem__:obj['属性'] 时触发
    __setitem__:obj['属性']=属性的值 时触发
    __delitem__:del obj['属性'] 时触发
     1 #   ########################### 只适用于操作字典 ###############
     2 class Func:
     3     def __getitem__(self, item):
     4         print("执行getitem")
     5         return self.__dict__[item]
     6 
     7     def __setitem__(self, key, value):
     8         print("执行setitem")
     9         self.__dict__[key]=value
    10 
    11     def __delitem__(self, key):
    12         print("执行delitem")
    13         self.__dict__.pop(key)
    14 
    15 f1 = Func()
    16 print(f1.__dict__)
    17 f1["name"] = "xiaoming"
    18 f1["age"] = 18
    19 print("====>",f1.__dict__)
    20 
    21 del f1["name"]
    22 print(f1.__dict__)
    23 
    24 print(f1["age"])
    三种item操作

    六 描述符(__get__,__set__,__delete__) 新式类中描述符在大型开发中常用

    1 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一个,这也被称为描述符协议

    __get__():调用一个属性时,触发
    __set__():为一个属性赋值时,触发
    __delete__():采用del删除属性时,触发

    1 class Foo: #在python3中Foo是新式类,它实现了三种方法,这个类就被称作一个描述符
    2     def __get__(self, instance, owner):
    3         pass
    4     def __set__(self, instance, value):
    5         pass
    6     def __delete__(self, instance):
    7         pass
    定义一个描述符

    2 描述符是干什么的:描述符的作用是用来代理另外一个类的属性的(必须把描述符定义成这个类的类属性,不能定义到构造函数中)

     1 class Foo:
     2     def __get__(self, instance, owner):
     3         print('触发get')
     4     def __set__(self, instance, value):
     5         print('触发set')
     6     def __delete__(self, instance):
     7         print('触发delete')
     8 
     9 #包含这三个方法的新式类称为描述符,由这个类产生的实例进行属性的调用/赋值/删除,并不会触发这三个方法
    10 f1=Foo()
    11 f1.name='egon'
    12 f1.name
    13 del f1.name
    14 #疑问:何时,何地,会触发这三个方法的执行
    引子:描述符类产生的实例进行属性操作并不会触发三个方法的执
     1 #描述符Str
     2 class Str:
     3     def __get__(self, instance, owner):
     4         print('Str调用')
     5     def __set__(self, instance, value):
     6         print('Str设置...')
     7     def __delete__(self, instance):
     8         print('Str删除...')
     9 
    10 #描述符Int
    11 class Int:
    12     def __get__(self, instance, owner):
    13         print('Int调用')
    14     def __set__(self, instance, value):
    15         print('Int设置...')
    16     def __delete__(self, instance):
    17         print('Int删除...')
    18 
    19 class People:
    20     name=Str()
    21     age=Int()
    22     def __init__(self,name,age): #name被Str类代理,age被Int类代理,
    23         self.name=name
    24         self.age=age
    25 
    26 #何地?:定义成另外一个类的类属性
    27 
    28 #何时?:且看下列演示
    29 
    30 p1=People('alex',18)
    31 
    32 #描述符Str的使用
    33 p1.name
    34 p1.name='egon'
    35 del p1.name
    36 
    37 #描述符Int的使用
    38 p1.age
    39 p1.age=18
    40 del p1.age
    41 
    42 #我们来瞅瞅到底发生了什么
    43 print(p1.__dict__)
    44 print(People.__dict__)
    45 
    46 #补充
    47 print(type(p1) == People) #type(obj)其实是查看obj是由哪个类实例化来的
    48 print(type(p1).__dict__ == People.__dict__)
    49 
    50 描述符应用之何时?何地?
    描述符应用之何时?何地?

    3 描述符分两种
    一 数据描述符:至少实现了__get__()和__set__()

    二 非数据描述符:没有实现__set__()

    4 注意事项:
    一 描述符本身应该定义成新式类,被代理的类也应该是新式类
    二 必须把描述符定义成这个类的类属性,不能为定义到构造函数中
    三 要严格遵循该优先级,优先级由高到底分别是
    1.类属性
    2.数据描述符
    3.实例属性
    4.非数据描述符
    5.找不到的属性触发__getattr__()

    类属性>数据描述符
     1 #有get,set就是数据描述符,数据描述符比实例属性有更高的优化级
     2 
     3 class Foo:
     4     def __get__(self,instance,owner):
     5         print('===>get方法')
     6     def __set__(self, instance, value):
     7         print('===>set方法')
     8     def __delete__(self, instance):
     9         print('===>delete方法')
    10 
    11 class Bar:
    12     x = Foo()  # 调用foo()属性,会触发get方法
    13 
    14 b1=Bar()   #在自己的属性字典里面找,找不到就去类里面找,会触发__get__方法
    15 b1.x       #调用一个属性的时候触发get方法
    16 b1.x=1     #为一个属性赋值的时候触发set方法
    17 del b1.x   #采用del删除属性时触发delete方法
    数据描述符>实例属性
     1 #类属性>数据描述符>实例属性
     2 
     3 class Foo:
     4     def __get__(self,instance,owner):
     5         print('===>get方法')
     6     def __set__(self, instance, value):
     7         print('===>set方法')
     8     def __delete__(self, instance):
     9         print('===>delete方法')
    10 
    11 class Bar:
    12     x = Foo()             #调用foo()属性,会触发get方法
    13 
    14 b1=Bar()                  #实例化
    15 Bar.x=11111111111111111   #不会触发get方法
    16 b1.x                      #会触发get方法
    17 
    18 del Bar.x                 #已经给删除,所以调用不了!报错:AttributeError: 'Bar' object has no attribute 'x'
    19 b1.x
    类属性>数据描述符>实例属性
     1 #实例属性>非数据描述符
     2 class Foo:
     3     def __get__(self,instance,owner):
     4         print('===>get方法')
     5 
     6 class Bar:
     7     x = Foo()
     8 
     9 b1=Bar()
    10 b1.x=1
    11 print(b1.__dict__)  #在自己的属性字典里面,{'x': 1}
    实例属性>非数据描述符
     1 class Foo:
     2     def __get__(self,instance,owner):
     3         print('===>get方法')
     4 
     5 class Bar:
     6     x = Foo()
     7     def __getattr__(self, item):
     8         print('------------>')
     9 
    10 b1=Bar()
    11 b1.xxxxxxxxxxxxxxxxxxx    #调用没有的xxxxxxx,就会触发__getattr__方法
    非数据描述符>找不到

    5 描述符使用

    众所周知,python是弱类型语言,即参数的赋值没有类型限制,下面我们通过描述符机制来实现类型限制功能

    A.让我么先来学习一下类的装饰器

     1 def deco(obj):
     2     print(">>>>>",obj)
     3     obj.x = 1
     4     obj.y = 2
     5     obj.z = 3
     6     return obj
     7 @deco  # Foo = deco(Foo)
     8 class Foo:
     9     pass
    10 print(Foo.__dict__)
    1、类的装饰器基本原理
     1 # 参数版的装饰器
     2 def Type(**kwargs):
     3     def deco(obj):
     4         for key,val in kwargs.items():
     5             setattr(obj,key,val)
     6         print("====>",kwargs)
     7         print("类名",obj)
     8         return obj
     9     print(">>>",kwargs)
    10     return deco
    11 @Type(x= 1,y = 2)  # Type(x=1,y=2)  ---->@deco ---->Foo = deco(Foo)
    12 class Foo:
    13     pass
    14 print(Foo.__dict__)
    带参数的类的装饰器
     1 def Typed(**kwargs):
     2     def deco(obj):
     3         print(">>>",obj)
     4         for key,val in kwargs.items():
     5             setattr(obj,key,val)
     6         return obj
     7     return deco
     8 
     9 @Typed(x=1)     #  Typed(x=1)  >>>> @deco 2 @deco ----> Foo = deco(Foo)
    10 class Foo:
    11     pass
    12 print(Foo.__dict__)
    3类的装饰器终极版

    B.实现类型限制功能

     1 class Typed:
     2     # def __init__(self,key):
     3     #     self.k = key
     4     def __get__(self, instance, owner):
     5         print("get方法")
     6         print("instance参数【%s】" %instance)  # instance参数【<__main__.People object at 0x0000020661BF4438>】
     7         print("owner参数【%s】" % owner)  # owner参数【<class '__main__.People'>】
     8     def __set__(self, instance, value):
     9         print("set方法")
    10         print("instance参数【%s】" % instance) #instance参数【<__main__.People object at 0x0000020661BF4438>】
    11         print("value参数【%s】" % value) #value参数【alex】
    12 
    13     def __delete__(self, instance):
    14         print("set方法")
    15         print("instance参数【%s】" % instance)
    16 
    17 class People:
    18     name = Typed()
    19     def __init__(self,name,age,slary):
    20         self.name =name
    21         self.age = age
    22         self.slary = slary
    23 p1 = People("alex",36,743782.3321)  # 触发 __set__
    24 p1.name  # 触发__get__
    25 p1.name = "xiaoming"
    26 print(p1.__dict__)  # {'slary': 743782.3321, 'age': 36}
    初探
     1 class Typed:
     2     def __init__(self,key,expect_type):
     3         self.key = key
     4         self.expect_type = expect_type
     5     def __get__(self, instance, owner):
     6         print("get方法")
     7         return instance.__dict__[self.key]
     8     def __set__(self, instance, value):
     9         print("set方法")
    10         # print("instance参数【%s】" % instance) #instance参数【<__main__.People object at 0x0000020661BF4438>】
    11         # print("value参数【%s】" % value) #value参数【alex】
    12         if not isinstance(value,self.expect_type):
    13             raise TypeError(" %s 传入的不是%s"%(self.key,self.expect_type))
    14             # return
    15         instance.__dict__[self.key] = value
    16     def __delete__(self, instance):
    17         print("set方法")
    18         # print("instance参数【%s】" % instance)
    19 
    20 class People:
    21     name = Typed("name",str)
    22     age = Typed("age",int)
    23     def __init__(self,name,age,slary):
    24         self.name =name
    25         self.age = age
    26         self.slary = slary
    27 p1 = People("alex",36,743782.3321)  # 触发 __set__
    28 print(p1.__dict__)
    29 print(p1.name)
    30 # p1 = People(213,11,22)
    31 print(p1.__dict__)  # {'slary': 743782.3321, 'age': 36}
    实现类型检测
     1 ######################## 类的装饰器和描述符应用结合 ####################
     2 
     3 class Typed:
     4     def __init__(self,key,expect_type):
     5         self.key = key
     6         self.expect_type = expect_type
     7     def __get__(self, instance, owner):
     8         print("get方法")
     9         return instance.__dict__[self.key]
    10     def __set__(self, instance, value):
    11         print("set方法")
    12         if not isinstance(value,self.expect_type):
    13             raise TypeError(" %s传入的不是%s"%(self.key,self.expect_type))
    14             # return
    15         instance.__dict__[self.key] = value
    16     def __delete__(self, instance):
    17         print("set方法")
    18 
    19 def Type(**kwargs):
    20     def deco(obj):
    21         print(">>>",obj)
    22         for name,expect_type in kwargs.items():
    23             setattr(obj,name,Typed(name,expect_type))
    24         return obj
    25     return deco
    26 @Type(name = str ,age = int)
    27 class People:
    28     # name = Typed("name",str)
    29     # age = Typed("age",int)
    30     def __init__(self,name,age,slary):
    31         self.name =name
    32         self.age = age
    33         self.slary = slary
    34 p1 = People("alex",22,743782.3321)  # 触发 __set__
    35 print(p1.__dict__)
    36 # p1 = People(213,11,22)
    37 print(p1.__dict__)  # {'slary': 743782.3321, 'age': 36}
    高级版,类的装饰器和描述符结合

    6 描述符总结

    描述符是可以实现大部分python类特性中的底层魔法,包括@classmethod,@staticmethd,@property甚至是__slots__属性

    描述父是很多高级库和框架的重要工具之一,描述符通常是使用到装饰器或者元类的大型框架中的一个组件

    7 利用描述符原理完成一个自定制@property,实现延迟计算(本质就是把一个函数属性利用装饰器原理做成一个描述符:类的属性字典中函数名为key,value为描述符类产生的对象)

    class Lazyproperty:
        def __init__(self,func):
            print(self)  # <__main__.Lazyproperty object at 0x000002181F574470>
            print("======>",func)  # ======> <function Room.area at 0x0000014B6274F2F0>
            self.func = func
        def __get__(self, instance, owner):
            print(self)  # <__main__.Lazyproperty object at 0x000001DD19624470>
            print("get")
            print(instance) # <__main__.Room object at 0x0000027CC4054470>
            print(owner)  # <class '__main__.Room'>
            return self.func(instance)
    class Room:
        def __init__(self,name,width,length):
            self.name = name
            self.width = width
            self.length = length
        # @property
        @Lazyproperty   #  area = Lazyproperty(area)  # 就相当于把area是Lazyproperty类的对象 # 实例化对象
        def area(self):
            return self.width * self.length
    r1 = Room('厕所',1,2)
    print(r1.area)
    自定制@property
     1 # 手动添加版
     2 class Lazyproperty:
     3     def __init__(self,func):
     4         # print(self)   #  <__main__.Lazyproperty object at 0x000001DBBA094518>
     5         # print(func) # <function Room.area at 0x000001DBBA08F510>
     6         self.func = func
     7 
     8 class Room:
     9     def __init__(self,name,width,length):
    10         self.name = name
    11         self.width = width
    12         self.length = length
    13     @Lazyproperty   # area = Lazypeoprety(area)  当于实例化Lazypeoprety
    14     def area(self):
    15         return self.width * self.length
    16     # @property
    17     # def area1(self):
    18     #     return self.width * self.length
    19 
    20 r1 = Room("cesuo",1,2)
    21 print(r1.area) # <__main__.Lazyproperty object at 0x000001FF19504438>
    22 print(r1.area.func)  # <function Room.area at 0x000002BB3093F488>
    23 print(r1.area.func(r1))  # 2 手动添加版
    手动添加版
     1 class Lazyproperty:
     2     def __init__(self,func):
     3         # print(self)   #  <__main__.Lazyproperty object at 0x000001DBBA094518>
     4         # print(func) # <function Room.area at 0x000001DBBA08F510>
     5         self.func = func
     6     def __get__(self, instance, owner):
     7         print("get")
     8         if instance is None:
     9             return self
    10         res = self.func(instance)
    11         setattr(instance,self.func.__name__,res)
    12         return res
    13 
    14 class Room:
    15     def __init__(self,name,width,length):
    16         self.name = name
    17         self.width = width
    18         self.length = length
    19     @Lazyproperty   # area = Lazypeoprety(area)  当于实例化Lazypeoprety
    20     def area(self):
    21         return self.width * self.length
    22     @property
    23     def area1(self):
    24         return self.width * self.length
    25 
    26 r1 = Room("cesuo",1,2)
    27 print(r1.area)
    28 print(r1.__dict__)
    29 print(r1.area)
    30 print(r1.area)
    31 print(r1.area)
    32 
    33 print(r1.area1)
    实现延迟计算功能

    七 再看property

    一个静态属性property本质就是实现了get,set,delete三种方法

     1 class Foo:
     2     @property
     3     def AAA(self):
     4         print('get的时候运行我啊')
     5 
     6     @AAA.setter
     7     def AAA(self,value):
     8         print('set的时候运行我啊')
     9 
    10     @AAA.deleter
    11     def AAA(self):
    12         print('delete的时候运行我啊')
    13 
    14 #只有在属性AAA定义property后才能定义AAA.setter,AAA.deleter
    15 f1=Foo()
    16 f1.AAA
    17 f1.AAA='aaa'
    18 del f1.AAA
    19 
    20 用法一
    用法一
     1 class Foo:
     2     def get_AAA(self):
     3         print('get的时候运行我啊')
     4 
     5     def set_AAA(self,value):
     6         print('set的时候运行我啊')
     7 
     8     def delete_AAA(self):
     9         print('delete的时候运行我啊')
    10     AAA=property(get_AAA,set_AAA,delete_AAA) #内置property三个参数与get,set,delete一一对应
    11 
    12 f1=Foo()
    13 f1.AAA
    14 f1.AAA='aaa'
    15 del f1.AAA
    用法二

    用法

     1 class Goods:
     2 
     3     def __init__(self):
     4         # 原价
     5         self.original_price = 100
     6         # 折扣
     7         self.discount = 0.8
     8 
     9     @property
    10     def price(self):
    11         # 实际价格 = 原价 * 折扣
    12         new_price = self.original_price * self.discount
    13         return new_price
    14 
    15     @price.setter
    16     def price(self, value):
    17         self.original_price = value
    18 
    19     @price.deleter
    20     def price(self):
    21         del self.original_price
    22 
    23 
    24 obj = Goods()
    25 obj.price         # 获取商品价格
    26 obj.price = 200   # 修改商品原价
    27 print(obj.price)
    28 del obj.price     # 删除商品原价
    案例一
     1 #实现类型检测功能
     2 
     3 #第一关:
     4 class People:
     5     def __init__(self,name):
     6         self.name=name
     7 
     8     @property
     9     def name(self):
    10         return self.name
    11 
    12 # p1=People('alex') #property自动实现了set和get方法属于数据描述符,比实例属性优先级高,所以你这面写会触发property内置的set,抛出异常
    13 
    14 
    15 #第二关:修订版
    16 
    17 class People:
    18     def __init__(self,name):
    19         self.name=name #实例化就触发property
    20 
    21     @property
    22     def name(self):
    23         # return self.name #无限递归
    24         print('get------>')
    25         return self.DouNiWan
    26 
    27     @name.setter
    28     def name(self,value):
    29         print('set------>')
    30         self.DouNiWan=value
    31 
    32     @name.deleter
    33     def name(self):
    34         print('delete------>')
    35         del self.DouNiWan
    36 
    37 p1=People('alex') #self.name实际是存放到self.DouNiWan里
    38 print(p1.name)
    39 print(p1.name)
    40 print(p1.name)
    41 print(p1.__dict__)
    42 
    43 p1.name='egon'
    44 print(p1.__dict__)
    45 
    46 del p1.name
    47 print(p1.__dict__)
    48 
    49 
    50 #第三关:加上类型检查
    51 class People:
    52     def __init__(self,name):
    53         self.name=name #实例化就触发property
    54 
    55     @property
    56     def name(self):
    57         # return self.name #无限递归
    58         print('get------>')
    59         return self.DouNiWan
    60 
    61     @name.setter
    62     def name(self,value):
    63         print('set------>')
    64         if not isinstance(value,str):
    65             raise TypeError('必须是字符串类型')
    66         self.DouNiWan=value
    67 
    68     @name.deleter
    69     def name(self):
    70         print('delete------>')
    71         del self.DouNiWan
    72 
    73 p1=People('alex') #self.name实际是存放到self.DouNiWan里
    74 p1.name=1
    案例二

    八 __getattribute__

     1 # ######################getattr##################
     2 # class Foo:
     3 #     def __init__(self,x):
     4 #         self.x = x
     5 #     def __getattr__(self, item):
     6 #         print("执行getattr")  # 如果对象没有实例调用不到执行
     7 #
     8 # f1 = Foo(10)
     9 # print(f1.x)
    10 # f1.ccc   #  触发getattr    执行getattr
    __getattr__
     1 # ######################### getattribute #################
     2 
     3 # class Foo:
     4 #     def __init__(self,x):
     5 #         self.x = x
     6 #     def __getattribute__(self, item):
     7 #         print("执行getattribute")  # 不管有没有都会触发
     8 #
     9 # f1 = Foo(10)
    10 # print(f1.x)
    11 # f1.ccc
    __getattribute__
     1 # ###############3 两者同时存在 #######################
     2 class Foo:
     3     def __init__(self,x):
     4         self.x = x
     5     def __getattr__(self, item):
     6         print("执行getattr")  # 如果对象没有实例调用不到执行
     7     def __getattribute__(self, item):
     8         print("执行getattribute")
     9         raise AttributeError('抛出异常')
    10 
    11 
    12 f1 = Foo(10)
    13 print(f1.x)
    14 # f1.ccc
    View Code

    九 __str__,__repr__,__format__

    改变对象的字符串显示__str__,__repr__

    自定制格式化字符串__format__

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name = name
     4         self.age = age
     5     def __str__(self):
     6         return  "名字是 %s 年龄是%s"%(self.name,self.age)
     7 
     8 f1 = Foo("alex",18)
     9 print(f1)  # 就相当于执行   str(f1) -----> f1.__str__()
    10 
    11 class Foo:
    12     def __init__(self,name,age):
    13         self.name = name
    14         self.age = age
    15     # def __str__(self):
    16         # return  "这是str"
    17     def __repr__(self):
    18         return  "名字是 %s 年龄是%s"%(self.name,self.age)
    19 
    20 f1 = Foo("alex",18)
    21 print(f1)  # 就相当于执行   str(f1) -----> f1.__str__()
    22 
    23 
    24 # str函数或者print函数--->obj.__str__()
    25 # repr或者交互式解释器--->obj.__repr__()
    26 # 如果__str__没有被定义,那么就会使用__repr__来代替输出
    27 # 注意:这俩方法的返回值必须是字符串,否则抛出异常
    str 和repr
     1 # ############################### format ###############################3
     2 # format_dic = {
     3 #     "ymd" :'{0.year}{0.mon}{0.day3}',
     4 #     "y:m:d" :'{0.year}:{0.mon}:{0.day3}',
     5 #     "m-d-y":'{0.mon}-{0.day3}-{0.year}'
     6 # }   # 利用字符串拼接搞成一个字典
     7 #
     8 # class Date:
     9 #     def __init__(self,year,mon,day):
    10 #         self.year = year
    11 #         self.mon = mon
    12 #         self.day = day
    13 #     def __format__(self, format_spec):   # 执行format()的时候自动执行 d1.__format__
    14 #         print("执行format")
    15 #         if not format_spec or format_spec not in format_dic:  # 判断format_spec 传的是否是空,和 传过来的参数是否在字典中
    16 #             format_spec = "ymd"
    17 #         fmt = format_dic[format_spec]   # 取字典key值
    18 #         return fmt.format(self)   #  '{0.year}{0.mon}{0.day3}'.format(self=d1)
    19 # d1 = Date(2016,12,26)
    20 # while True:
    21 #     forma = input("请输入你要查看的格式q退出")
    22 #     if forma.lower() =="q" :
    23 #         break
    24 #     print(format(d1,forma))
    format

    十 __slots__

    1.__slots__是什么?是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性)

    2.引子:使用点来访问属性本质就是在访问类或者对象的__dict__属性字典(类的字典是共享的,而每个实例的是独立的)

    3.为何使用__slots__:字典会占用大量内存,如果你有一个属性很少的类,但是有很多实例,为了节省内存可以使用__slots__取代实例的__dict__ 当你定义__slots__后,__slots__就会为实例使用一种更加紧凑的内部表示。实例通过一个很小的固定大小的数组来构建,而不是为每个实例定义一个 字典,这跟元组或列表很类似。在__slots__中列出的属性名在内部被映射到这个数组的指定小标上。使用__slots__一个不好的地方就是我们不能再给 实例添加新的属性了,只能使用在__slots__中定义的那些属性名。

    4.注意事项:__slots__的很多特性都依赖于普通的基于字典的实现。另外,定义了__slots__后的类不再 支持一些普通类特性了,比如多继承。大多数情况下,你应该 只在那些经常被使用到 的用作数据结构的类上定义__slots__比如在程序中需要创建某个类的几百万个实例对象 。 关于__slots__的一个常见误区是它可以作为一个封装工具来防止用户给实例增加新的属性。尽管使用__slots__可以达到这样的目的,但是这个并不是它的初衷。更多的是用来作为一个内存优化工具。

    __slots__的作用:节省内存空间

     1 #  ##### __slots__ 省内存 ,只能创建自己定义的参数 #####
     2 class Foo:
     3     __slots__ = "name"
     4 
     5 f1 = Foo()
     6 # print(f1.__dict__)  #Foo' object has no attribute '__dict__'
     7 print(f1.__slots__)
     8 f1.name = "alex"
     9 # print(f1.name)
    10 
    11 # f1.age = 18   #  报错
    __slots__

    十一 __next__和__iter__实现迭代器协议

    一、什么是迭代器协议

    1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退)

    2.可迭代对象:实现了迭代器协议的对象(如何实现:对象内部定义一个__iter__()方法)

    3.协议是一种约定,可迭代对象实现了迭代器协议,python的内部工具(如for循环,sum,min,max函数等)使用迭代器协议访问对象。 

    二、python中强大的for循环机制

    for循环的本质:循环所有对象,全都是使用迭代器协议。

    (字符串,列表,元组,字典,集合,文件对象)这些都不是可迭代对象,只不过在for循环式,调用了他们内部的__iter__方法,把他们变成了可迭代对象

    然后for循环调用可迭代对象的__next__方法去取值,而且for循环会捕捉StopIteration异常,以终止迭代。

     1 # ################### 迭代器协议 ##################
     2 class Foo:
     3     def __init__(self,n):
     4         self.n = n
     5     def __iter__(self):
     6         return self
     7     def __next__(self):
     8         if self.n == 13 :
     9             raise StopIteration("终止了")
    10         self.n += 1
    11         return self.n
    12 f1 = Foo(10)
    13 print(f1.__next__())
    14 print(f1.__next__())
    15 print(f1.__next__())
    16 # print(f1.__next__())  # StopIteration: 终止了
    17 
    18 for i in f1:  # obj = iter(f1) ------>f1.__iter__()
    19     print(i)  # obj.__next()
    迭代器协议
     1 class Fib:
     2     def __init__(self):
     3         self._a=0
     4         self._b=1
     5 
     6     def __iter__(self):
     7         return self
     8 
     9     def __next__(self):
    10         self._a,self._b=self._b,self._a + self._b
    11         return self._a
    12 
    13 f1=Fib()
    14 
    15 print(f1.__next__())
    16 print(next(f1))
    17 print(next(f1))
    18 
    19 for i in f1:
    20     if i > 100:
    21         break
    22     print('%s ' %i,end='')
    斐波那契数列

    十二 __doc__ 文档描述信息

    1 class Foo:
    2     '我是描述信息'
    3     pass
    4 
    5 print(Foo.__doc__)
    描述信息
    1 class Foo:
    2     '我是描述信息'
    3     pass
    4 
    5 class Bar(Foo):
    6     pass
    7 print(Bar.__doc__) #该属性无法继承给子类
    该属性无法被继承

    十三 __module__和__class__

    __module__ 表示当前操作的对象在那个模块

    __class__     表示当前操作的对象的类是什么

    1 class C:
    2 
    3     def __init__(self):
    4         self.name = ‘SB'
    5 
    6 lib/aa.py
    创建lib/aa.py
    1 1 from lib.aa import C
    2 2 
    3 3 obj = C()
    4 4 print obj.__module__     #输出 lib.aa,即:输出模块
    5 5 print obj.__class__      #输出 lib.aa.C,即:输出类
    2、输出模块和输出类

    十四  __del__

    析构方法,当对象在内存中被释放时,自动触发执行。

    注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的。

     1 # class Foo:
     2 #     def __init__(self,name):
     3 #         self.name = name
     4 #     def __del__(self):
     5 #         print("我执行了")
     6 # f1 = Foo("alex")
     7 # # del f1  # 删除实例会触发__del__
     8 # del f1.name  # 删除实例的属性不会触发__del__
     9 # print("#################")
    10 #
    11 # #  程序运行完毕会自动回收内存,触发__del__
    12 a = 1
    13 b = 2
    14 a,b =b,a
    15 print(a)
    16 print(b
    示范

    十五 __enter__和__exit__

    1、操作文件写法

    1 with open('a.txt') as f:
    2   '代码块'
    

    2、上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明__enter__和__exit__方法

     1 # class Open:
     2 #     def __init__(self,name):
     3 #         self.name = name
     4 #     def __enter__(self):
     5 #         print("执行enter")
     6 #         return self
     7 #     def __exit__(self, exc_type, exc_val, exc_tb):
     8 #         print("执行exit")
     9 # #  会自动触发__enter__
    10 # #  文件操作完之后会触发 __exit__
    11 # with Open("a.text") as f :
    12 #     print(f)
    13 #     print(f.name)   # __enter__ 的返回值赋值给f
    14 #     print(">>>>>")
    15 #     print(">>>>>")
    16 #
    17 # print("s".center(20,"*"))
    上下文管理协议

    3、执行代码块

    __exit__()中的三个参数分别代表异常类型,异常值和追溯信息,with语句中代码块出现异常,则with后的代码都无法执行

    没有异常的情况下,整个代码块运行完毕后去触发__exit__,它的三个参数都会执行

     1 class Foo:
     2     def __init__(self,name):
     3         self.name=name
     4 
     5     def __enter__(self):
     6         print('执行enter')   
     7         return self        #2、拿到的结果是self,并赋值给f
     8 
     9     def __exit__(self, exc_type, exc_val, exc_tb):  #4、触发__exit__,然后执行print()
    10         print('执行exit')
    11         print(exc_type)
    12         print(exc_val)
    13         print(exc_tb)
    14         
    15 
    16 with Foo('a.txt') as f:  #1、with触发的是__enter__,拿到的结果是self并赋值给f; 
    17     print(f)             #3、然后会执行with代码块,执行完毕后
    18     print(assfsfdsfdsfdsfffsadfdsafdsafdsafdsafdsafdsafdsafdsad)  
    19     print(f.name)      
    20 print('000000000000000000000000000000000000000000000000000000')
    View Code

    4、有返回值

    如果__exit()返回值为True,那么异常会被清空,就好像啥都没发生一样,with后的语句正常执行

     1 class Foo:
     2     def __init__(self,name):
     3         self.name=name
     4 
     5     def __enter__(self):
     6         print('执行enter')   
     7         return self
     8                        '''class、异常值、追踪信息'''
     9     def __exit__(self, exc_type, exc_val, exc_tb):   #2、有异常的时候,就会触发__exit__方法
    10         print('执行exit')
    11         print(exc_type)
    12         print(exc_val)
    13         print(exc_tb)
    14         return True         #3、没有return True就会报错,如果有return True异常自己吃了,不报异常
    15 
    16 with Foo('a.txt') as f:
    17     print(f)                
    18     print(assfsfdsfdsfdsfffsadfdsafdsafdsafdsafdsafdsafdsafdsad)    #1、有异常的情况,他就会触发__exit__
    19     print(f.name)           #不执行这行,直接打印下面那行
    20 print('000000000000000000000000000000000000000000000000000000')     #4、最后打印这行
    View Code

    总结:

    with obj as f:
          '代码块'

    1.with obj ---->触发obj.__enter__(),拿到返回值

    2.as f----->f=返回值、

    3.with obj as f 等同于 f=obj.__enter__()

    4.执行代码块
    一:没有异常的情况下,整个代码块运行完毕后去触发__exit__,它的三个参数都为None
    二:有异常的情况下,从异常出现的位置直接触发__exit__
    a:如果__exit__的返回值为True,代表吞掉了异常
    b:如果__exit__的返回值不为True,代表吐出了异常
    c:__exit__的的运行完毕就代表了整个with语句的执行完毕

    用途:

    1.使用with语句的目的就是把代码块放入with中执行,with结束后,自动完成清理工作,无须手动干预

    2.在需要管理一些资源比如文件,网络连接(TCP协议建连接、传输数据、关连接)和锁(进程,线程)的编程环境中,可以在__exit__中定制自动释放资源的机制,你无须再去关系这个问题,这将大有用处。

    十六 __call__

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

    注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()

    1 class Foo:
    2     def __call__(self, *args, **kwargs):
    3         print("实例执行 obj()")
    4 f1 = Foo()
    5 f1()  #   实例执行 obj()
    6 Foo()()   # 实例执行 obj()
    __call__

    十七 metaclass

    1、示例:

    1 class Foo:
    2     pass
    3 
    4 f1=Foo() #f1是通过Foo类实例化的对象

    python中一切皆是对象,类本身也是一个对象,当使用关键字class的时候,python解释器在加载class的时候就会创建一个对象(这里的对象指的是类而非类的实例)

    上例可以看出f1是由Foo这个类产生的对象,而Foo本身也是对象,那它又是由哪个类产生的呢?

     1 class Foo:
     2     pass
     3 
     4 f1=Foo()
     5 
     6 print(type(f1))  #<class '__main__.Foo'>  
     7 print(type(Foo)) #类的类就是<class 'type'>
     8 
     9 class Bar:
    10     pass   
    11 print(type(Bar))   #<class 'type'>
    type

    2、什么是元类?

    元类是类的类,是类的模板

    元类是用来控制如何创建类的,正如类是创建对象的模板一样

    元类的实例为类,正如类的实例为对象(f1对象是Foo类的一个实例Foo类是 type 类的一个实例)

    type是python的一个内建元类,用来直接控制生成类,python中任何class定义的类其实都是type类实例化的对象

    3、创建类的两种方式

    1 class Foo:
    2     pass
    3 print(Foo)
    4 print(Foo.__dict__)
    方法一
     1 #type就是类的类,用type()实例化的结果,就是产生一个类
     2 #用type和class生成的类是一样的效果。
     3 
     4 def __init__(self,name,age):
     5     self.name=name
     6     self.age=age
     7 
     8 def test(self):
     9     print('=======执行的是test=====>')
    10 
    11 FFo=type('FFo',(object,),{'x':1,'__init__':__init__,'test':test})
    12 print(FFo)
    13 # print(FFo.__dict__)
    14 
    15 f1=FFo('alex',18)
    16 print(f1.x)         #调的是类属性
    17 print(f1.name)      #调name
    18 f1.test()           #调方法
    方法二

    4、一个类没有声明自己的元类,默认他的元类就是type,除了使用元类type,用户也可以通过继承type来自定义元类。 

    示例:  自定制元类

     1 class MyType(type):
     2     def __init__(self,a,b,c):
     3         print('元类的构造函数执行')
     4 
     5     def __call__(self, *args, **kwargs):
     6         obj=object.__new__(self)            #object.__new__(Foo)-->f1
     7         self.__init__(obj,*args,**kwargs)   #Foo.__init__(f1,*arg,**kwargs)
     8         return obj
     9 
    10 class Foo(metaclass=MyType):   #Foo=MyType(Foo,'Foo',(),{})---》__init__
    11     def __init__(self,name):
    12         self.name=name          #f1.name=name
    13 f1=Foo('alex')
    自定制元类

     十八:异常处理

    查看请点击:异常处理

  • 相关阅读:
    PAT 1008--------数组元素的循环右移,你需要记住的
    PAT1049-----枚举法,找规律题,注意降低时间复杂度
    PAT1048----你需要了解并记住的解题思路
    C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar()))
    PAT1040----关于数学题目的解法新思路值得借鉴,字符的配对
    PAT1029-----介绍字符串的解题思路和部分知识点
    PAT1027-----等差数列的问题或数学问题
    PAT1026----四舍五入的思路,%2d的一些知识
    / 已阅 /PAT1017-------高精度计算,问题的所有可能情况
    LeetCode 无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/sunkai1993/p/6225377.html
Copyright © 2011-2022 走看看