当前位置:网站首页>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
When the attribute search is not found ( Report errors AttriErro) when , This method is triggered
When looking for attributes , This method will be called at the first time
When setting properties , This method will be triggered to set properties
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
边栏推荐
- A copy can also produce flowers
- [network security interview question] - how to penetrate the test file directory through
- Dachang image library
- MySQL winter vacation self-study 2022 11 (9)
- [matlab] access of variables and files
- Codeworks 5 questions per day (1700 average) - day 6
- How to read excel, PDF and JSON files in R language?
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
- XSS challenges绕过防护策略进行 XSS 注入
- Network Security Learning - Web vulnerabilities (Part 1)
猜你喜欢
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
Introduction to robotframework (II) app startup of appui automation
Universal crud interface
ReferenceError: primordials is not defined错误解决
C # create self host webservice
主数据管理(MDM)的成熟度
MySQL advanced notes
力扣今日题-729. 我的日程安排表 I
1. Dynamic parameters of function: *args, **kwargs
【指针训练——八道题】
随机推荐
A copy can also produce flowers
Referenceerror: primordials is not defined error resolution
DDoS attacks - are we really at war?
[kubernetes series] learn the exposed application of kubernetes service security
Differences and usage scenarios between TCP and UDP
Redis skip table
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
Software design principles
2022.02.13
[ruoyi] set theme style
Summary of Bible story reading
[ruoyi] ztree custom icon (iconskin attribute)
Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
Large scale DDoS attacks take Myanmar offline
How to accurately identify master data?
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
Is there a completely independent localization database technology
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
1. Dynamic parameters of function: *args, **kwargs
建模规范:命名规范