__getattr__
时间: 2023-09-03 21:14:29 浏览: 102
引用和介绍了__getattr__魔法函数的作用。当调用对象的属性不存在时,会触发__getattr__魔法函数,可以在此函数中进行定制化处理。例如,在引用中的代码示例中,当调用s.age时,由于age属性不存在,会触发__getattr__函数,输出"no age exist..."并返回0。而在引用的代码示例中,当调用x.name时,由于name属性不存在,会触发__getattr__函数,并且因为name属性不是age,所以会抛出AttributeError异常。所以,__getattr__函数可以用来处理调用属性不存在的异常情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [Python----魔法函数__getattr__/__setattr__/__delattr__/__getattribute__的用法](https://blog.csdn.net/redrose2100/article/details/118121685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Python的__getattr__方法学习](https://blog.csdn.net/qdPython/article/details/110233337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文