当前位置:网站首页>Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method

Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method

2022-07-06 02:51:00 chuntian_ tester

 

Catalog

1.object.__getattr__

         When the attribute search is not found ( Report errors AttriErro) when , This method is triggered

2.object.__getattribute__

         When looking for attributes , This method will be called at the first time

3.object.__setattr__

         When setting properties , This method will be triggered to set properties

4.object.__delattr__

        stay del Object properties , This method is triggered


Define magic methods to define attribute access of class instances .

summary : When the method of the parent class cannot meet its functional requirements , You can override the parent method , When rewriting, add the functions you want to add , adopt super(). Parent class method () To extend the function of the parent method , such , Rewrite your own method , It has the function of parent method , It also has its own added functions .

1.object.__getattr__

         When the attribute search is not found ( Report errors AttriErro) when , This method is triggered

class Attr:

    def __getattr__(self, item):
        #  When accessing object properties , If the attribute does not exist , And an error AttrError when , This method is triggered .
        print('---getattr--- Method triggered ')
        return 666


if __name__ == '__main__':
    a = Attr()
    print(a.name)
    #  Output :
    # ---getattr - -- Method triggered 
    # 666

2.object.__getattribute__

         When looking for attributes , This method will be called at the first time

class Attr:

    def __getattr__(self, item):
        #  When accessing object properties , If the attribute does not exist , And an error AttrError when , This method is triggered .
        print('---getattr--- Method triggered ')
        #  Call the parent class when triggered getattribute Method , Find properties , If not , Throw out  AttributeError: 'Attr' object has no attribute 'name'
        object.__getattribute__(self, item)


    def __getattribute__(self, item):
        #  When accessing properties , This method will be triggered for the first time to find properties 
        print('---getattribute--- Method triggered ')

        #  Add custom function 
        print(' Custom function is added here ')

        #  After adding the customization function , Calling the getattribute Method to find properties 
        return super().__getattribute__(item)



if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    print(a.name)
    #  Output :
    ---getattribute - -- Method triggered 
     Custom function is added here 
     Springfield 

3.object.__setattr__

         When setting properties , This method will be triggered to set properties

class Attr:

    def __setattr__(self, key, value):
        #  When setting properties for objects , This method is triggered 
        print('---setattr--- Method triggered ')
        print(key)
        print(value)


if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    #  Output :
    ---setattr - -- Method triggered 
    name
     Springfield 
class Attr:

    def __setattr__(self, key, value):
        #  When setting properties for objects , This method is triggered 
        print('---setattr--- Method triggered ')
        if key == 'name':
            #  Locked object name attribute , Cannot be modified outside the object , Regardless of the outside a.name Set to how much ,name Properties are never modified 
            super().__setattr__(key, " Sheep Baa Baa ")
        else:
            #  The object property set is not name, It can be modified normally 
            super().__setattr__(key, value)


if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    print(a.name)
    a.age = 130
    print(a.age)
    #  Output :
    ---setattr - -- Method triggered 
     Sheep Baa Baa 
    ---setattr - -- Method triggered 
    130

4.object.__delattr__

        stay del Object properties , This method is triggered

class Attr:

    def __delattr__(self, item):
        #  This method will be triggered when deleting attributes 
        print('---delattr--- Method triggered ')


if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    del a.name
    #  Output :
    ---delattr - -- Method triggered 
class Attr:

    def __delattr__(self, item):
        #  This method will be triggered when deleting attributes 
        print('---delattr--- Method triggered ')
        #  Extend the functionality 
        print(' Here are the extended functions ')
        #  Calling the delattr Method to delete a property 
        super().__delattr__(item)
        print(f' attribute {item} It has been deleted ')


if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    del a.name
    print(a.name)
    #  Output :
    ---delattr - -- Method triggered 
     Here are the extended functions 
     attribute name It has been deleted 
    AttributeError: 'Attr' object has no attribute 'name'
class Attr:

    def __delattr__(self, item):
        #  This method will be triggered when deleting attributes 
        print('---delattr--- Method triggered ')
        #  Extend the functionality , If the attribute you want to delete is name, Just refuse , Can't delete 
        if item == 'name':
            print(f'{item} Attribute cannot be deleted ')    #  Protect name Properties are not deleted 
        else:
            #  Calling the delattr Method to delete a property 
            super().__delattr__(item)
            print(f' attribute {item} It has been deleted ')


if __name__ == '__main__':
    a = Attr()
    a.name = ' Springfield '
    del a.name
    print(a.name)
    #  Output :
    ---delattr - -- Method triggered 
    name Attribute cannot be deleted 
     Springfield 

 

原网站

版权声明
本文为[chuntian_ tester]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132348590158.html