{"id":325634,"date":"2021-02-06T19:09:33","date_gmt":"2021-02-06T11:09:33","guid":{"rendered":"http:\/\/4563.org\/?p=325634"},"modified":"2021-02-06T19:09:33","modified_gmt":"2021-02-06T11:09:33","slug":"python-%e7%b1%bb%e5%9e%8b%e6%b3%a8%e8%a7%a3%ef%bc%8c-enum-%e8%bf%98%e6%98%af-str-literal%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=325634","title":{"rendered":"Python \u7c7b\u578b\u6ce8\u89e3\uff0c Enum \u8fd8\u662f str Literal\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u7c7b\u578b\u6ce8\u89e3\uff0c Enum \u8fd8\u662f str Literal\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Contextualist <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u5728\u7528 <code>attrs<\/code> \u505a TOML \u914d\u7f6e\u7684\u7ed3\u6784\u5b9a\u4e49\u3002\u9047\u5230\u6709\u4e9b\u6761\u76ee\u662f\u8054\u5408\u7c7b\u578b\uff0c\u53ef\u4ee5\u6307\u5b9a\u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7ed9\u5177\u4f53\u6570\u503c\uff0c\u6bd4\u5982\u4ee5\u4e0b TOML\uff1a<\/p>\n<pre><code>attr0 = 0.98 # \u6216\u8005 #attr0 = \"magicx\" <\/code><\/pre>\n<p>\u90a3\u4e48\u8fd9\u4e2a\u6761\u76ee\u7684\u5b9a\u4e49\u6709\u4e24\u79cd\u5199\u6cd5\uff0c\u7528 <code>str Literal<\/code>\uff1a<\/p>\n<pre><code>import attr from typing import Union, Literal  @attr.s(auto_attribs=True) class Config:     attr0: Union[float, Literal[\"magicx\"]]  ... config = parse_config() if config.attr0 == \"magicx\":     ... <\/code><\/pre>\n<p>\u6216\u8005\u7528 <code>Enum<\/code>\uff1a<\/p>\n<pre><code>import attr from typing import Union from enum import Enum  class Attr0KW(Enum):     MAGICX = \"magicx\"  @attr.s(auto_attribs=True) class Config:     attr0: Union[float, Attr0KW]  ... config = parse_config() if config.attr0 is Attr0KW.MAGICX:     ... <\/code><\/pre>\n<p>\u50cf\u8fd9\u79cd\u53ea\u6709\u4e00\u4e2a\u5173\u952e\u5b57\u7684\uff0c\u5199\u4e2a Enum \u53c8\u89c9\u5f97\u8fc7\u5ea6\u5c01\u88c5\u4e86\uff0c\u4f46\u7528 str Literal \u53c8\u89c9\u5f97\u4e0d\u4e25\u8c28\u3002\u5e94\u8be5\u5982\u4f55\u53d6\u820d\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5213832\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : karatsuba <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u77e5\u9053\uff0c\u4e0d\u7528 toml                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5213833\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crclz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90fd\u53ef\u4ee5\u3002\u50cf JAVA C#\u8fd9\u79cd\u7684\uff0c\u7528 ENUM \u662f\u6807\u51c6\u64cd\u4f5c\u3002\u6240\u4ee5 python \u7528 enum \u7b97\u4e0d\u4e0a\u8fc7\u5ea6\u5c01\u88c5\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u4f60\u8fd9\u4e2a\u7c7b\u7684\u5f00\u53d1\u8005\u5f88\u591a\uff0c\u90a3\u4e48\u5c31\u5c3d\u91cf\u4f7f\u7528 Enum \u3002\u5982\u679c\u81ea\u5df1\u7528\uff0c\u5c31\u4f7f\u7528\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u6211\u4e2a\u4eba\u7684\u5efa\u8bae\u662f\uff0c\u5176\u5b9e python \u4f5c\u4e3a\u4e00\u95e8\u52a8\u6001\u8bed\u8a00\uff0c\u4e5f\u8be5\u6709\u52a8\u6001\u8bed\u8a00\u7684\u6837\u5b50\uff0c\u5c31\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\u5c31\u884c\u4e86\uff0c\u7ea6\u675f\u4ec0\u4e48\u7684\u4e5f\u6ca1\u6709\u5fc5\u8981\u7528\u7c7b\u578b\u6ce8\u89e3\u6765\u505a\uff0c\u5f71\u54cd\u5f00\u53d1\u901f\u5ea6\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u8fd9\u662f\u6709\u4f9d\u636e\u7684\uff0c\u4f8b\u5982 numpy \u7684 padding=&#8217;zero&#8217;\u4e4b\u7c7b\u7684\u4f20\u53c2\uff0c\u5927\u5bb6\u90fd\u6ca1\u600e\u4e48\u62b1\u6028\u8fd9\u79cd\u8bbe\u8ba1\u3002\u5982\u679c\u53c2\u6570\u4e0d\u77e5\u9053\u586b\u4ec0\u4e48\uff0c\u5927\u5bb6\u90fd\u77e5\u9053\u53bb\u770b\u51fd\u6570\u6ce8\u91ca\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7c7b\u578b\u6ce8\u89e3\uff0c Enum&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/325634"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=325634"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/325634\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=325634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=325634"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=325634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}