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

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

      Python字符串属性

      时间:2023-09-13

      • <legend id='s982M'><style id='s982M'><dir id='s982M'><q id='s982M'></q></dir></style></legend>

              <bdo id='s982M'></bdo><ul id='s982M'></ul>
              <tfoot id='s982M'></tfoot>
                <tbody id='s982M'></tbody>
            • <small id='s982M'></small><noframes id='s982M'>

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

                本文介绍了Python字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我怎样才能完成这样的工作:

                How can I achieve such job:

                def get_foo(someobject, foostring):
                    return someobject.foostring
                

                IE:

                如果我这样做 get_foo(obj, "name") 它应该调用 obj.name (将输入视为字符串,但我将其称为 attritube.

                if I do get_foo(obj, "name") it should be calling obj.name (see input as string but I call it as an attritube.

                谢谢

                推荐答案

                使用内置函数 getattr.

                Use the builtin function getattr.

                getattr(object, name[, default])

                返回object的命名属性的值.name 必须是字符串.如果字符串是对象属性之一的名称,则结果是该属性的值.例如,getattr(x, 'foobar') 等价于 x.foobar.如果命名属性不存在,则返回default,否则返回AttributeError 被引发.

                Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

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

                上一篇:遍历python中的对象属性 下一篇:为什么不能在 python 中为对象添加属性?

                相关文章

                最新文章

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

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

                    • <bdo id='nXBsD'></bdo><ul id='nXBsD'></ul>
                    <legend id='nXBsD'><style id='nXBsD'><dir id='nXBsD'><q id='nXBsD'></q></dir></style></legend>
                    <tfoot id='nXBsD'></tfoot>