当前位置:网站首页>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
边栏推荐
- 微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
- Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
- 淘宝焦点图布局实战
- Pat 1046 shortest distance (20 points) simulation
- XSS challenges绕过防护策略进行 XSS 注入
- How to improve the enthusiasm of consumers when the member points marketing system is operated?
- JS events (add, delete) and delegates
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
- 故障分析 | MySQL 耗尽主机内存一例分析
- Introduction to robotframework (III) Baidu search of webui automation
猜你喜欢
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
Introduction to robotframework (I) brief introduction and use
RobotFramework入门(二)appUI自动化之app启动
2022.02.13
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
Microservice registration and discovery
Universal crud interface
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
随机推荐
主数据管理(MDM)的成熟度
Gifcam v7.0 minimalist GIF animation recording tool Chinese single file version
不赚钱的科大讯飞,投资价值该怎么看?
Codeforces 5 questions par jour (1700 chacune) - jour 6
Differences and usage scenarios between TCP and UDP
RobotFramework入门(三)WebUI自动化之百度搜索
2022.02.13
Prototype design
MySQL winter vacation self-study 2022 11 (9)
Elimination games
2.11 simulation summary
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
Force buckle 146 LRU cache
The difference between sizeof and strlen in C language
My C language learning records (blue bridge) -- files and file input and output
C # create self host webservice
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
Dachang image library
Large scale DDoS attacks take Myanmar offline
【若依(ruoyi)】启用迷你导航栏