zoukankan      html  css  js  c++  java
  • python中常用的内置方法

    类的内置方法(魔法方法):

    凡是在类内部定义,以__开头__结尾的方法,都是类的内置方法,类的内置方法,会在满足某种条件下自动触发

    __new__:在___init__触发前,自动触发。调用该类时,内部会通过__new__产生一个新对象
        
        __init__:在调用类时自动触发。通过产生的对象自动调用__init__()
    
    class Demo(object):
    
        # 条件: __new__: 在__init__触发前,自动触发。
        def __new__(cls,*args,**kwargs):
            print('此处是__new__方法的执行')
            #python内部通过object调用的__new__实现产生一个空的对象 --->内存地址
            return object.__new__(cls,*args,**kwargs)
    
        #条件:__init__在调用类时自动触发
        def __init__(self):
            print('此处是__init__方法的执行')
    
    Demo()
    __getattr__: 在通过 “对象.属性” 获取属性时,若 “属性没有” 时触发。
        
    class Demo(object):
        # x =10
    
        def __getattr__(self,item):
            print('此处是__getattr__方法的执行')
            print(item)
            #return 想要返回的值
            return 123
    obj1 = Demo()
    print(obj1.x)
    
    #执行结果:
    此处是__getattr__方法的执行
    x
    123
    条件: __getattribute__: 在通过 “对象.属性” 获取属性时,无论 "属性有没有" 都会触发执行。
    
    class Demo:
        #x=10
        def __getattr__(self, item):
            print('此处是__getattr__方法的执行')
    
        def __getattribute__(self, item):
            print('此处是__getattribute__方法的执行')
            # 注意: 此处不能通过对象.属性,否则会产生递归调用,程序崩溃
            # return self.__dict__[item]
            print(item, '<-----打印属性名字')
            # raise AttributeError('抛出异常了')
    obj1 = Demo()
    obj1.x
    
    # 注意: 只要__getattr__ 与 __getattribute__ 同时存在类的内部,只会触发__getattribute__。
    #若 执行到__getattribute__ 发现raise AttributeError('抛出异常了'),则再触发__getattr__
    条件: __setattr__当 “对象.属性 = 属性值” , 添加或修改属性时触发执行。
    条件:__delattr__当删除 “对象.属性” 属性的时候会触发执行。
    
    class Demo:
        def __setattr__(self, key, value): # key---> 对象.属性名  value ---》 属性值
            print('此处是__setattr__方法的执行')
            print(key, value)
            # 出现递归
            # self.key = value
            # 此处是对 对象的名称空间 ---》 字典进行操作
            self.__dict__[key] = value
    obj1 = Demo()
    obj1.x =10
    # 原来设置属性时,会自动触发父类中的__setattr__,内部为对象添加x属性,值为20
    print(obj1.__dict__)
    
    #执行结果:
    此处是__setattr__方法的执行
    x 10
    {'x': 10}
    
    
    
    class Demo:
        x = 1
        def __init__(self,y):
            self.y = y
    
        def __delattr__(self, item):
            print('此处是__delattr__方法的执行')
    
    obj1 = Demo(10)
    del obj1.y
    del obj1.x
    
    #执行结果:
    此处是__delattr__方法的执行
    此处是__delattr__方法的执行
    l =list('hello')  #l是list类的实例化出来的一个实例,即一个对象
    print(l)   #['h','e','l','l','o']
    
    class Foo:
        pass
    f1 = Foo()
    print(f1)  #<__main__.Foo object at 0x0000022735920688>
    条件: __str__在打印对象时触发。
    
    注意: 该方法必须要有一个 “字符串” 返回值。
    
    class Demo:
        def __str__(self):
            print('此处是__str__方法的执行')
            return '自定制的对象的显示方式'  
    
    obj1 =Demo()
    print(obj1)
    
    #执行结果:
    此处是__str__方法的执行
    自定制的对象的显示方式
    条件:__getitem__在对象通过 “对象[key]” 获取属性时触发。
    
    class Demo:
        def __init__(self,name):
            self.name=name
        def __getitem__(self, item):
            print('此处是__getitem__方法的执行')
            print(item)
            return self.__dict__[item]
    
    obj1 = Demo('lili')
    print(obj1, '<----- 打印的对象')
    print(obj1['name'])
    条件:__setitem__在对象通过 “对象[key]=value值” 设置属性时触发。
    
    class Demo:
        def __setitem__(self, key, value):
            print('此处是__setitem__方法的执行')
            self.__dict__[key] = value
    
    obj1 = Demo()
    print(obj1.__dict__)
    obj1['name'] = 'baohan'
    print(obj1.__dict__)
    
    #执行结果:
    {}
    此处是__setitem__方法的执行
    {'name': 'baohan'}
    条件:__delitem__在对象通过 del “对象[key]” 属性时触发。
    class Demo:
        def __setitem__(self, key, value):
            print('此处是__setitem__方法的执行')
            self.__dict__[key] = value
    
        def __delitem__(self, key):
            print('此处是__delitem__方法的执行')
            self.__dict__.pop(key)
    
    obj1 = Demo()
    print(obj1.__dict__)
    obj1['name'] = 'baohan'
    print(obj1.__dict__)
    #del obj1.name
    del obj1['name']
    print(obj1.__dict__)
    
    #执行结果:
    {}
    此处是__setitem__方法的执行
    {'name': 'baohan'}
    此处是__delitem__方法的执行
    {}
    条件: __call__在调用对象 “对象 + ()” 时触发。
    
    class Demo:
        def __call__(self, *args, **kwargs):
            print('此处是__call__方法的执行')
            # 调用对象时返回的值
            return [1,2,3,4]
    obj1 =Demo()
    obj1()
  • 相关阅读:
    Comparable VS Comparator
    Javascript中this关键字详解
    Runtime、System、Object
    JS IDE
    异常处理
    Throwable vs Exception
    8.4 Java 命名规范
    关键字、标识符、注释、变量
    Docker —— 从入门到实践
    RTC教程
  • 原文地址:https://www.cnblogs.com/wddxx/p/13671442.html
Copyright © 2011-2022 走看看