python中__dict__的实例属性存储
小妮浅浅
2021-05-26 09:19:586109浏览 · 0收藏 · 0评论
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'}
以上就是python中__dict__的实例属性存储,希望对大家有所帮助。更多Python学习推荐:python教学
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
关注公众号,随时随地在线学习