<tfoot id='XxMG1'></tfoot>

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

    1. <legend id='XxMG1'><style id='XxMG1'><dir id='XxMG1'><q id='XxMG1'></q></dir></style></legend>

      1. <small id='XxMG1'></small><noframes id='XxMG1'>

        在python中从字典中设置属性

        时间:2023-09-13
          <bdo id='fYVEl'></bdo><ul id='fYVEl'></ul>

                <tfoot id='fYVEl'></tfoot>
              1. <small id='fYVEl'></small><noframes id='fYVEl'>

                  <tbody id='fYVEl'></tbody>
                <legend id='fYVEl'><style id='fYVEl'><dir id='fYVEl'><q id='fYVEl'></q></dir></style></legend>

                <i id='fYVEl'><tr id='fYVEl'><dt id='fYVEl'><q id='fYVEl'><span id='fYVEl'><b id='fYVEl'><form id='fYVEl'><ins id='fYVEl'></ins><ul id='fYVEl'></ul><sub id='fYVEl'></sub></form><legend id='fYVEl'></legend><bdo id='fYVEl'><pre id='fYVEl'><center id='fYVEl'></center></pre></bdo></b><th id='fYVEl'></th></span></q></dt></tr></i><div id='fYVEl'><tfoot id='fYVEl'></tfoot><dl id='fYVEl'><fieldset id='fYVEl'></fieldset></dl></div>
                  本文介绍了在python中从字典中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以从 python 中的字典中创建一个对象,使得每个键都是该对象的一个​​属性?

                  Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object?

                  类似这样的:

                   d = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
                  
                   e = Employee(d) 
                   print e.name # Oscar 
                   print e.age + 10 # 42 
                  

                  我认为这几乎与这个问题相反:Python 字典对象的字段

                  I think it would be pretty much the inverse of this question: Python dictionary from an object's fields

                  推荐答案

                  当然,是这样的:

                  class Employee(object):
                      def __init__(self, initial_data):
                          for key in initial_data:
                              setattr(self, key, initial_data[key])
                  

                  更新

                  正如 Brent Nash 建议的那样,您也可以通过允许关键字参数来使其更加灵活:

                  As Brent Nash suggests, you can make this more flexible by allowing keyword arguments as well:

                  class Employee(object):
                      def __init__(self, *initial_data, **kwargs):
                          for dictionary in initial_data:
                              for key in dictionary:
                                  setattr(self, key, dictionary[key])
                          for key in kwargs:
                              setattr(self, key, kwargs[key])
                  

                  那么你可以这样称呼它:

                  Then you can call it like this:

                  e = Employee({"name": "abc", "age": 32})
                  

                  或者像这样:

                  e = Employee(name="abc", age=32)
                  

                  甚至像这样:

                  employee_template = {"role": "minion"}
                  e = Employee(employee_template, name="abc", age=32)
                  

                  这篇关于在python中从字典中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么不能在 python 中为对象添加属性? 下一篇:如何在 Windows 中使用 Python 删除只读属性目录?

                  相关文章

                  最新文章

                • <tfoot id='RcIem'></tfoot>

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

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

                        <bdo id='RcIem'></bdo><ul id='RcIem'></ul>