python中__dict__的实例属性存储

354次阅读
没有评论

python中__dict__的实例属性存储

1、在Python中,所有实例属性都存储在_dict__字典中,这是通常的dict,实例属性的维护是从这个字典中获得和修正,对开发者完全开放。

>>> e = Employee('IT', 'bobo')
>>> e.__dict__
{'department': 'IT', 'name': 'bobo'}
>>> type(e.__dict__)
dict
>>> e.name is e.__dict__['name']
True
>>> e.__dict__['department'] = 'HR'
>>> e.department
'HR'

2、实例属性是用字典存储的,所以可以随时方便地为对象添加或删除字段:

>>> e.age = 30 # 并没有定义 age 属性
>>> e.age
30
>>> e.__dict__
{'department': 'IT', 'name': 'bobo', 'age': 30}
>>> del e.age
>>> e.__dict__
{'department': 'IT', 'name': 'd'}
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-01发表,共计461字。
新手QQ群:570568346,欢迎进群讨论 Python51学习