<legend id='6FwOH'><style id='6FwOH'><dir id='6FwOH'><q id='6FwOH'></q></dir></style></legend>
    <bdo id='6FwOH'></bdo><ul id='6FwOH'></ul>

<i id='6FwOH'><tr id='6FwOH'><dt id='6FwOH'><q id='6FwOH'><span id='6FwOH'><b id='6FwOH'><form id='6FwOH'><ins id='6FwOH'></ins><ul id='6FwOH'></ul><sub id='6FwOH'></sub></form><legend id='6FwOH'></legend><bdo id='6FwOH'><pre id='6FwOH'><center id='6FwOH'></center></pre></bdo></b><th id='6FwOH'></th></span></q></dt></tr></i><div id='6FwOH'><tfoot id='6FwOH'></tfoot><dl id='6FwOH'><fieldset id='6FwOH'></fieldset></dl></div>

<tfoot id='6FwOH'></tfoot>
  • <small id='6FwOH'></small><noframes id='6FwOH'>

      1. 从标准库向 Python 字典添加属性

        时间:2023-09-13

            <tbody id='PyAn7'></tbody>
            • <bdo id='PyAn7'></bdo><ul id='PyAn7'></ul>

                <i id='PyAn7'><tr id='PyAn7'><dt id='PyAn7'><q id='PyAn7'><span id='PyAn7'><b id='PyAn7'><form id='PyAn7'><ins id='PyAn7'></ins><ul id='PyAn7'></ul><sub id='PyAn7'></sub></form><legend id='PyAn7'></legend><bdo id='PyAn7'><pre id='PyAn7'><center id='PyAn7'></center></pre></bdo></b><th id='PyAn7'></th></span></q></dt></tr></i><div id='PyAn7'><tfoot id='PyAn7'></tfoot><dl id='PyAn7'><fieldset id='PyAn7'></fieldset></dl></div>

                  <small id='PyAn7'></small><noframes id='PyAn7'>

                  <legend id='PyAn7'><style id='PyAn7'><dir id='PyAn7'><q id='PyAn7'></q></dir></style></legend>

                  <tfoot id='PyAn7'></tfoot>
                  本文介绍了从标准库向 Python 字典添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想知道是否可以向 Python 字典添加属性?

                  I was wondering if you could add an attribute to a Python dictionary?

                  class myclass():
                      def __init__(): 
                       self.mydict = {}  # initialize a regular dict
                       self.mydict.newattribute = "A description of what this dictionary will hold"
                       >>> AttributeError: 'dict' object has no attribute 'newattribute'
                       setattr(self.mydict, "attribute", "A description of what this dictionary will hold"
                       >>> AttributeError: 'dict' object has no attribute 'newattribute'
                  

                  有没有办法快速添加我的描述属性,而不必复制 dict 类和重载构造函数.我以为这很简单,但我想我错了.

                  Is there anyway to quickly add my description attribute without having to copy the dict class and overloading the constructor. I thought it would be simple, but I guess I was wrong.

                  推荐答案

                  直接派生自dict:

                  class MyDict(dict):
                      pass
                  

                  MyDict 实例的行为类似于 dict,但可以具有自定义属性:

                  Instances of MyDict behave like a dict, but can have custom attributes:

                  >>> d = MyDict()
                  >>> d.my_attr = "whatever"
                  >>> d.my_attr
                  'whatever'
                  

                  这篇关于从标准库向 Python 字典添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:__getattribute__ 方法和描述符 下一篇:Python 类属性及其初始化

                  相关文章

                  最新文章

                  1. <legend id='08wcd'><style id='08wcd'><dir id='08wcd'><q id='08wcd'></q></dir></style></legend>
                  2. <tfoot id='08wcd'></tfoot>
                  3. <small id='08wcd'></small><noframes id='08wcd'>

                  4. <i id='08wcd'><tr id='08wcd'><dt id='08wcd'><q id='08wcd'><span id='08wcd'><b id='08wcd'><form id='08wcd'><ins id='08wcd'></ins><ul id='08wcd'></ul><sub id='08wcd'></sub></form><legend id='08wcd'></legend><bdo id='08wcd'><pre id='08wcd'><center id='08wcd'></center></pre></bdo></b><th id='08wcd'></th></span></q></dt></tr></i><div id='08wcd'><tfoot id='08wcd'></tfoot><dl id='08wcd'><fieldset id='08wcd'></fieldset></dl></div>

                        <bdo id='08wcd'></bdo><ul id='08wcd'></ul>