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

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

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

      在 Python 中将字符串转换为枚举

      时间:2023-09-14
            <tfoot id='oN2Ai'></tfoot>

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

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

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

                <legend id='oN2Ai'><style id='oN2Ai'><dir id='oN2Ai'><q id='oN2Ai'></q></dir></style></legend>
                  <tbody id='oN2Ai'></tbody>
                本文介绍了在 Python 中将字符串转换为枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想知道将字符串转换(反序列化)为 Python 的 Enum 类的正确方法是什么.似乎 getattr(YourEnumType, str) 可以完成这项工作,但我不确定它是否足够安全.

                I wonder what's the correct way of converting (deserializing) a string to a Python's Enum class. Seems like getattr(YourEnumType, str) does the job, but I'm not sure if it's safe enough.

                更具体地说,我想将 'debug' 字符串转换为 Enum 对象,如下所示:

                Just to be more specific, I would like to convert a 'debug'string to an Enum object like this:

                class BuildType(Enum):
                    debug = 200
                    release = 400
                

                推荐答案

                此功能已内置于 Enum [1]:

                This functionality is already built in to Enum [1]:

                >>> from enum import Enum
                >>> class Build(Enum):
                ...   debug = 200
                ...   build = 400
                ... 
                >>> Build['debug']
                <Build.debug: 200>
                

                成员名称区分大小写,因此如果正在转换用户输入,您需要确保大小写匹配:

                The member names are case sensitive, so if user-input is being converted you need to make sure case matches:

                an_enum = input('Which type of build?')
                build_type = Build[an_enum.lower()]
                

                [1] 官方文档:枚举编程访问

                [1] Official docs: Enum programmatic access

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

                上一篇:“无法将'int'对象隐式转换为str"错误 下一篇:阻止 Pandas 将 int 转换为 float

                相关文章

                最新文章

              • <small id='HhW3B'></small><noframes id='HhW3B'>

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

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