• 技术文章 >Python技术 >Python基础教程

    python中__dict__的实例属性存储

    小妮浅浅小妮浅浅2021-05-26 09:19:58原创5021

    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电脑。

    专题推荐:python __dict__
    品易云
    上一篇:python decimal是什么 下一篇:__dict__在python中的实例操作

    相关文章推荐

    • defaultdict在python中计算键值的和• python中OrdereDict如何使用?• 在python中defaultdict计算键值的和• python中dict有哪些删除的方式• defaultdict在python中接收调用对象• OrderedDict在python字典的实现• python中defaultdict的初始化

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网