zoukankan      html  css  js  c++  java
  • 描述符

    描述符

    class Foo:
        def __get__(self, instance, owner):
            print('===>get方法')
        def __set__(self, instance, value):
            print('===>set方法',instance,value)
            instance.__dict__['x']=value #b1.__dict__
        def __delete__(self, instance):
            print('===>delete方法')
    
    
    class Bar:
        x=Foo() #在何地?
        def __init__(self,n):
            self.x=n #b1.x=10
    b1=Bar(10)
    print(b1.__dict__)
    b1.x=11111111111111111
    print(b1.__dict__)
    
    b1.y=11111111111111111111111111111111111111
    print(b1.__dict__)
    # print(Bar.__dict__)
    #在何时?
    # b1=Bar()
    # b1.x
    #
    # b1.x=1
    #
    # del b1.x
    
    # print(b1.x)
    #
    # b1.x=1
    # print(b1.__dict__)
    #
    # del b1.x

    描述符优先级

    # class Foo:
    #     def __get__(self, instance, owner):
    #         print('===>get方法')
    #     def __set__(self, instance, value):
    #         print('===>set方法',instance,value)
    #         # instance.__dict__['x']=value #b1.__dict__
    #     def __delete__(self, instance):
    #         print('===>delete方法')
    #
    #
    # class Bar:
    #     x=Foo() #在何地?
    
    # print(Bar.x)
    
    # Bar.x=1
    # print(Bar.__dict__)
    # print(Bar.x)
    
    # print(Bar.__dict__)
    # b1=Bar()
    # b1.x   #get
    # b1.x=1 # set
    # del b1.x # delete
    
    
    # b1=Bar()
    # Bar.x=111111111111111111111111111111111111111
    # b1.x
    #
    # del Bar.x
    # b1.x
    
    
    
    class Foo:
        def __get__(self, instance, owner):
            print('===>get方法')
    
    
        # def __delete__(self, instance):
        #     print('===>delete方法')
    
    
    class Bar:
        x=Foo() #在何地?
        def  __getattr__(self, item):
            print('----->')
    
    b1=Bar()
    b1.x=1
    print(b1.__dict__)
    b1.xxxxxxxxxxxxxxxxxxxxxxx

     描述符的应用:

    可以在实例化设值的时候 加入逻辑判断,如判断数据类型是否合法

    # def test(x):
    #     print('===>',x)
    #
    # test('alex')
    # test(111111)
    
    
    class Typed:
        def __init__(self,key,expected_type):
            self.key=key
            self.expected_type=expected_type
        def __get__(self, instance, owner):
            print('get方法')
            # print('instance参数【%s】' %instance)
            # print('owner参数【%s】' %owner)
            return instance.__dict__[self.key]
        def __set__(self, instance, value):
            print('set方法')
            # print('instance参数【%s】' % instance)
            # print('value参数【%s】' % value)
            # print('====>',self)
            if not isinstance(value,self.expected_type):
                # print('你传入的类型不是字符串,错误')
                # return
                raise TypeError('%s 传入的类型不是%s' %(self.key,self.expected_type))
            instance.__dict__[self.key]=value
        def __delete__(self, instance):
            print('delete方法')
            # print('instance参数【%s】' % instance)
            instance.__dict__.pop(self.key)
    
    class People:
        name=Typed('name',str) #t1.__set__()  self.__set__()
        age=Typed('age',int) #t1.__set__()  self.__set__()
        def __init__(self,name,age,salary):
            self.name=name
            self.age=age
            self.salary=salary
    
    # p1=People('alex','13',13.3)
    p1=People(213,13,13.3)
    
    # p1=People('alex',13,13.3)
    # print(p1.__dict__)
    # p1=People(213,13,13.3)
    # print(p1.__dict__)
    # print(p1.__dict__)
    # print(p1.name)
    
    # print(p1.__dict__)
    # p1.name='egon'
    # print(p1.__dict__)
    
    
    # print(p1.__dict__)
    # del p1.name
    # print(p1.__dict__)
    
    # print(p1)
    
    # print(p1.name)
    # p1.name='egon'
    # print(p1.name)
    # print(p1.__dict__)
  • 相关阅读:
    Laravel5 cookie和session设置
    php如何实现登陆后返回原页面
    laravel5项目安装debugbar
    phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪
    js 3秒后跳转页面的实现代码
    phpstorm常用plugins
    运用Xdebug调试和优化PHP程序
    Laravel5 打印SQL
    laravel 去掉index.php伪静态
    Linux gprof命令
  • 原文地址:https://www.cnblogs.com/jiawen010/p/10142224.html
Copyright © 2011-2022 走看看