当前位置:网站首页>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
边栏推荐
- 07 singleton mode
- MySQL winter vacation self-study 2022 11 (5)
- Codeforces 5 questions par jour (1700 chacune) - jour 6
- 张丽俊:穿透不确定性要靠四个“不变”
- Apt installation ZABBIX
- Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
- 建模规范:命名规范
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
- [ruoyi] enable Mini navigation bar
猜你喜欢
What is the investment value of iFLYTEK, which does not make money?
不赚钱的科大讯飞,投资价值该怎么看?
故障分析 | MySQL 耗尽主机内存一例分析
2345文件粉碎,文件强力删除工具无捆绑纯净提取版
Solve 9 with C language × 9 Sudoku (personal test available) (thinking analysis)
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
[pointer training - eight questions]
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
js 正则过滤和增加富文本中图片前缀
随机推荐
CSP date calculation
Universal crud interface
What is the investment value of iFLYTEK, which does not make money?
Introduction to robotframework (I) brief introduction and use
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
如何精准识别主数据?
Apt installation ZABBIX
MySQL advanced notes
2.13 simulation summary
How to accurately identify master data?
tcpdump: no suitable device found
微服务间通信
ERA5再分析资料下载攻略
QT release exe software and modify exe application icon
Redis skip table
js 正则过滤和增加富文本中图片前缀
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
MySQL winter vacation self-study 2022 11 (6)
Apt installation ZABBIX
codeforces每日5题(均1700)-第六天