{"id":204033,"date":"2025-05-29T09:52:51","date_gmt":"2025-05-29T01:52:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204033\/"},"modified":"2025-05-29T09:52:51","modified_gmt":"2025-05-29T01:52:51","slug":"python-%e4%b8%ad%e7%9a%84%e6%b1%82%e5%92%8c%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204033\/","title":{"rendered":"Python \u4e2d\u7684\u6c42\u548c\u7c7b\u578b"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d\u7684\u6c42\u548c\u7c7b\u578b<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython \u4e2d\u7684\u6c42\u548c\u7c7b\u578b\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241022\/17296045666717abd635df8.jpg\" class=\"aligncenter\"><\/p>\n<p>python \u662f\u4e00\u95e8\u53ef\u7231\u7684\u8bed\u8a00\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528 python \u65f6\uff0c\u6211\u7ecf\u5e38\u53d1\u73b0\u81ea\u5df1\u7f3a\u5c11\u5bf9\u603b\u548c\u7c7b\u578b\u7684\u5185\u7f6e\u652f\u6301\u3002\u50cf haskell \u548c rust \u8fd9\u6837\u7684\u8bed\u8a00\u8ba9\u8fd9\u79cd\u4e8b\u60c5\u53d8\u5f97\u5982\u6b64\u7b80\u5355\uff1a<\/p>\n<pre>data op = add | sub | mul\n  deriving (show)\n\ndata expr\n  = lit integer\n  | binop op expr expr\n  deriving (show)\n\nval :: expr -&gt; integer\nval (lit val) = val\nval (binop op lhs rhs) =\n  let x = val lhs\n      y = val rhs\n   in apply op x y\n\napply :: op -&gt; integer -&gt; integer -&gt; integer\napply add x y = x + y\napply sub x y = x - y\napply mul x y = x * y\n\nval (binop add (binop mul (lit 2) (lit 3)) (lit 4))\n-- =&gt; 10\n<\/pre>\n<p>\u867d\u7136 python \u4e0d\u652f\u6301\u8fd9\u79cd\u5f00\u7bb1\u5373\u7528\u7684\u6784\u9020\uff0c\u4f46\u6211\u4eec\u5c06\u770b\u5230\u50cf expr \u8fd9\u6837\u7684\u7c7b\u578b\u4ecd\u7136\u53ef\u4ee5\uff08\u5e76\u4e14\u5bb9\u6613\uff09\u8868\u8fbe\u3002\u6b64\u5916\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\u6765\u4e3a\u6211\u4eec\u5904\u7406\u6240\u6709\u4ee4\u4eba\u8ba8\u538c\u7684\u6837\u677f\u6587\u4ef6\u3002\u7ed3\u679c\u4e0e\u4e0a\u9762\u7684 haskell \u793a\u4f8b\u6ca1\u6709\u592a\u5927\u4e0d\u540c\uff1a<\/p>\n<pre># the `enum` decorator adds methods for constructing and matching on the\n# different variants:\n@enum(add=(), sub=(), mul=())\nclass op:\n    def apply(self, x, y):\n        return self.match(\n            add=lambda: x + y,\n            sub=lambda: x - y,\n            mul=lambda: x * y,\n        )\n\n\n# recursive sum types are also supported:\n@enum(lit=(int,), bin_op=lambda: (op, expr, expr))\nclass expr:\n    def val(self):\n        return self.match(\n            lit=lambda value: value,\n            bin_op=lambda op, lhs, rhs: op.apply(lhs.val(), rhs.val()),\n        )\n\n\nexpr.bin_op(\n    op.add(),\n    expr.bin_op(op.mul(), expr.lit(2), expr.lit(3)),\n    expr.lit(4)\n).val()\n# =&gt; 10\n<\/pre>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u201c\u6807\u8bb0\u8054\u5408\u201d\u6765\u8868\u793a\u603b\u548c\u7c7b\u578b\u3002\u901a\u8fc7\u793a\u4f8b\u5f88\u5bb9\u6613\u7406\u89e3\uff1a<\/p>\n<pre>class expr:\n    def lit(value):\n        e = expr()\n        e.tag = \"lit\"\n        e.value = value\n        return e\n\n    def bin_op(op, lhs, rhs):\n        e = expr()\n        e.tag = \"bin_op\"\n        e.op = op\n        e.lhs = lhs\n        e.rhs = rhs\n        return e\n<\/pre>\n<p>\u6bcf\u4e2a\u53d8\u4f53\u90fd\u662f\u540c\u4e00\u7c7b\u7684\u5b9e\u4f8b\uff08\u5728\u672c\u4f8b\u4e2d\u4e3a expr\uff09\u3002\u6bcf\u4e2a\u90fd\u5305\u542b\u4e00\u4e2a\u201c\u6807\u7b7e\u201d\uff0c\u6307\u793a\u5b83\u662f\u54ea\u4e2a\u53d8\u4f53\uff0c\u4ee5\u53ca\u7279\u5b9a\u4e8e\u5b83\u7684\u6570\u636e\u3002<\/p>\n<p>\u4f7f\u7528 expr \u7684\u6700\u57fa\u672c\u65b9\u6cd5\u662f\u4f7f\u7528 if-else \u94fe\uff1a<\/p>\n<pre>class expr:\n    # ...\n    def val(self):\n        if self.tag == \"lit\":\n            return self.value\n        elif self.tag == \"bin_op\":\n            x = self.lhs.val()\n            y = self.rhs.val()\n            return self.op.apply(x, y)\n<\/pre>\n<p>\u4f46\u662f\uff0c\u8fd9\u6709\u4e00\u4e9b\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u5728\u4f7f\u7528 expr \u7684\u6240\u6709\u5730\u65b9\u90fd\u4f1a\u91cd\u590d\u76f8\u540c\u7684 if-else \u94fe\u3002<\/li>\n<li>\u66f4\u6539\u6807\u7b7e\u7684\u503c\uff08\u4f8b\u5982\u4ece\u201clit\u201d\u5230\u201cliteral\u201d\uff09\u4f1a\u4e2d\u65ad \u73b0\u6709\u4ee3\u7801\u3002<\/li>\n<li>\u6d88\u8d39\u603b\u548c\u7c7b\u578b\u9700\u8981\u4e86\u89e3\u5b9e\u73b0\u7ec6\u8282\uff08\u5373\u6807\u7b7e\u548c \u6bcf\u4e2a\u53d8\u4f53\u4f7f\u7528\u7684\u5b57\u6bb5\u540d\u79f0\uff09\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u516c\u5f00\u7528\u4e8e\u6d88\u8017\u603b\u548c\u7c7b\u578b\u7684\u5355\u4e2a\u516c\u5171\u5339\u914d\u65b9\u6cd5\u6765\u907f\u514d\u6240\u6709\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n<pre>class expr:\n    # ...\n    def match(self, handlers):\n        # ...\n<\/pre>\n<p>\u4f46\u9996\u5148\u6211\u4eec\u9700\u8981\u4f7f\u4e0d\u540c\u7684\u53d8\u4f53\u66f4\u52a0\u7edf\u4e00\u3002\u6bcf\u4e2a\u53d8\u4f53\u73b0\u5728\u4e0d\u518d\u5c06\u5176\u6570\u636e\u5b58\u50a8\u5728\u5404\u4e2a\u5b57\u6bb5\u4e2d\uff0c\u800c\u662f\u5c06\u5176\u5b58\u50a8\u5728\u540d\u4e3a data \u7684\u5143\u7ec4\u4e2d\uff1a<\/p>\n<pre>class expr:\n    def lit(value):\n        e = expr()\n        e.tag = \"lit\"\n        e.data = (value,)\n        return e\n\n    def bin_op(op, lhs, rhs):\n        e = expr()\n        e.tag = \"bin_op\"\n        e.data = (op, lhs, rhs)\n        return e\n<\/pre>\n<p>\u8fd9\u4f7f\u6211\u4eec\u80fd\u591f\u5b9e\u73b0\u5339\u914d\uff1a<\/p>\n<pre>class expr:\n    # ...\n    def match(self, **handlers):\n        if self.tag in handlers:\n            return handlers[self.tag](*self.data)\n        else:\n            raise runtimeerror(f\"missing handler for {self.tag}\")\n<\/pre>\n<p>\u6211\u4eec\u4e00\u4e3e\u89e3\u51b3\u4e86\u4e0a\u8ff0\u6240\u6709\u95ee\u9898\uff01\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4e3a\u4e86\u6362\u4e2a\u73af\u5883\uff0c\u8fd9\u662f\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8f6c\u5f55\u7684 rust \u9009\u9879\u7c7b\u578b\uff1a<\/p>\n<pre>class option:\n    def some(x):\n        o = option()\n        o.tag = \"some\"\n        o.data = (x,)\n        return o\n\n    def none():\n        o = option()\n        o.tag = \"none\"\n        o.data = ()\n        return o\n\n    def match(self, **handlers):\n        if self.tag in handlers:\n            return handlers[self.tag](*self.data)\n        else:\n            raise runtimeerror(f\"missing handler for {self.tag}\")\n\n    def __repr__(self):\n        return self.match(\n            some=lambda x: f\"option.some({repr(x)})\",\n            none=lambda: \"option.none()\",\n        )\n\n    def __eq__(self, other):\n        if not isinstance(other, option):\n            return notimplemented\n        return self.tag == other.tag and self.data == other.data\n\n    def map(self, fn):\n        return self.match(\n            some=lambda x: option.some(fn(x)),\n            none=lambda: option.none()\n        )\n\noption.some(2).map(lambda x: x**2)\n# =&gt; option.some(4)\n<\/pre>\n<p>\u4f5c\u4e3a\u751f\u6d3b\u8d28\u91cf\u7684\u4e00\u9879\u5c0f\u798f\u5229\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5339\u914d\u4e2d\u652f\u6301\u7279\u6b8a\u7684\u901a\u914d\u7b26\u6216\u201c\u5305\u7f57\u4e07\u8c61\u201d\u7684\u5904\u7406\u7a0b\u5e8f\uff0c\u7528\u4e0b\u5212\u7ebf (_) \u8868\u793a\uff1a<\/p>\n<pre>def match(self, **handlers):\n    if self.tag in handlers:\n        return handlers[self.tag](*self.data)\n    elif \"_\" in handlers:\n        return handlers[\"_\"]()\n    else:\n        raise runtimeerror(f\"missing handler for {self.tag}\")\n<\/pre>\n<p>\u8fd9\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u5982\u4e0b\u5339\u914d\uff1a<\/p>\n<pre>def map(self, fn):\n    return self.match(\n        some=lambda x: option.some(fn(x)),\n        _=lambda: option.none(),\n    )\n<\/pre>\n<p>\u6b63\u5982 option \u7c7b\u6240\u793a\uff0c\u521b\u5efa\u603b\u548c\u7c7b\u578b\u6240\u9700\u7684\u8bb8\u591a\u4ee3\u7801\u90fd\u9075\u5faa\u76f8\u540c\u7684\u6a21\u5f0f\uff1a<\/p>\n<pre>class foo:\n    # for each variant:\n    def my_variant(bar, quux):\n        # construct an instance of the class:\n        f = foo()\n        # give the instance a distinct tag:\n        f.tag = \"my_variant\"\n        # save the values we received:\n        f.data = (bar, quux)\n        return f\n\n    # this is always the same:\n    def match(self, **handlers):\n        if self.tag in handlers:\n            return handlers[self.tag](*self.data)\n        elif \"_\" in handlers:\n            return handlers[\"_\"]()\n        else:\n            raise runtimeerror(f\"missing handler for {self.tag}\")\n<\/pre>\n<p>\u6211\u4eec\u4e0d\u7528\u81ea\u5df1\u7f16\u5199\u8fd9\u4e2a\uff0c\u800c\u662f\u7f16\u5199\u4e00\u4e2a<em>\u88c5\u9970\u5668<\/em>\u6765\u6839\u636e\u53d8\u4f53\u7684\u4e00\u4e9b\u63cf\u8ff0\u6765\u751f\u6210\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<pre>def enum(**variants):\n    pass\n<\/pre>\n<p>\u4ec0\u4e48\u6837\u7684\u63cf\u8ff0\uff1f\u6700\u7b80\u5355\u7684\u4e8b\u60c5\u662f\u63d0\u4f9b\u53d8\u4f53\u540d\u79f0\u5217\u8868\uff0c\u4f46\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u6211\u4eec\u671f\u671b\u7684\u53c2\u6570\u7c7b\u578b\u6765\u505a\u5f97\u66f4\u597d\u3002\u6211\u4eec\u5c06\u4f7f\u7528\u679a\u4e3e\u6765\u81ea\u52a8\u589e\u5f3a\u6211\u4eec\u7684 option \u7c7b\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre># add two variants:\n# - one named `some` that expects a single argument of any type.\n# - one named `none` that expects no arguments.\n@enum(some=(object,), none=())\nclass option:\n    pass\n<\/pre>\n<p>\u679a\u4e3e\u7684\u57fa\u672c\u7ed3\u6784\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>def enum(**variants):\n    def enhance(cls):\n        # add methods to the class cls.\n        return cls\n\n    return enhance\n<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u8fd4\u56de<em>\u53e6\u4e00\u4e2a<\/em>\u51fd\u6570\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u4f7f\u7528\u6211\u4eec\u6b63\u5728\u589e\u5f3a\u7684\u7c7b\u4f5c\u4e3a\u5176\u552f\u4e00\u53c2\u6570\u6765\u8c03\u7528\u3002\u5728\u589e\u5f3a\u4e2d\uff0c\u6211\u4eec\u5c06\u9644\u52a0\u7528\u4e8e\u6784\u5efa\u6bcf\u4e2a\u53d8\u4f53\u7684\u65b9\u6cd5\u4ee5\u53ca\u5339\u914d\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5339\u914d\uff0c\u56e0\u4e3a\u5b83\u53ea\u662f\u590d\u5236\u610f\u5927\u5229\u9762\uff1a<\/p>\n<pre>def enhance(cls):\n    def match(self, **handlers):\n        if self.tag in handlers:\n            return handlers[self.tag](*self.data)\n        elif \"_\" in handlers:\n            return handlers[\"_\"]()\n        else:\n            raise valueerror(f\"missing handler for {self.tag}\")\n\n    # add a method named \"match\" to the class cls, whose value is the\n    # `match` function defined above:\n    setattr(cls, \"match\", match)\n\n    return cls\n<\/pre>\n<p>\u6dfb\u52a0\u65b9\u6cd5\u6765\u6784\u9020\u6bcf\u4e2a\u53d8\u4f53\u53ea\u662f\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002\u6211\u4eec\u8fed\u4ee3\u53d8\u4f53\u5b57\u5178\uff0c\u4e3a\u6bcf\u4e2a\u6761\u76ee\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre>def enhance(cls):\n    # ...\n    for tag, sig in variants.items():\n        setattr(cls, tag, make_constructor(tag, sig))\n\n    return cls\n<\/pre>\n<p>\u5176\u4e2d make_constructor \u4e3a\u5e26\u6709\u6807\u7b7e\uff08\u548c\u540d\u79f0\uff09\u6807\u7b7e\u548c\u201c\u7c7b\u578b\u7b7e\u540d\u201dsig \u7684\u53d8\u4f53\u521b\u5efa\u6784\u9020\u51fd\u6570\uff1a<\/p>\n<pre>def enhance(cls):\n    # ...\n    def make_constructor(tag, sig):\n        def constructor(*data):\n            # validate the data passed to the constructor:\n            if len(sig) != len(data):\n                raise valueerror(f\"expected {len(sig)} items, not {len(data)}\")\n            for x, ty in zip(data, sig):\n                if not isinstance(x, ty):\n                    raise typeerror(f\"expected {ty} but got {repr(x)}\")\n\n            # just a generalization of what we've seen above:\n            inst = cls()\n            inst.tag = tag\n            inst.data = data\n            return inst\n\n        return constructor\n\n    for tag, sig in variants.items():\n        setattr(cls, tag, make_constructor(tag, sig))\n\n    return cls\n<\/pre>\n<p>\u8fd9\u91cc\u662f enum \u7684\u5b8c\u6574\u5b9a\u4e49\uff0c\u4f9b\u53c2\u8003\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 __repr__ \u548c __eq__ \u65b9\u6cd5\u8f7b\u677e\u589e\u5f3a\u6211\u4eec\u7684 sum \u7c7b\uff1a<\/p>\n<pre>def enhance(cls):\n    # ...\n    def _repr(self):\n        return f\"{cls.__name__}.{self.tag}({', '.join(map(repr, self.data))})\"\n\n    setattr(cls, \"__repr__\", _repr)\n\n    def _eq(self, other):\n        if not isinstance(other, cls):\n            return notimplemented\n        return self.tag == other.tag and self.data == other.data\n\n    setattr(cls, \"__eq__\", _eq)\n\n    return cls\n<\/pre>\n<p>\u901a\u8fc7\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u6539\u8fdb\u589e\u5f3a\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u4ee5\u6700\u5c0f\u7684\u65b9\u5f0f\u5b9a\u4e49\u9009\u9879\uff1a<\/p>\n<pre>@enum(some=(object,), none=())\nclass option:\n    def map(self, fn):\n        return self.match(\n            some=lambda x: option.some(fn(x)),\n            _=lambda: option.none(),\n        )\n<\/pre>\n<p>\u4e0d\u5e78\u7684\u662f\uff0c\u679a\u4e3e\uff08\u8fd8\uff09\u65e0\u6cd5\u5b8c\u6210\u5b9a\u4e49 expr \u7684\u4efb\u52a1\uff1a<\/p>\n<pre>@enum(add=(), sub=(), mul=())\nclass op:\n    pass\n\n@enum(lit=(int,), bin_op=(op, expr, expr))\nclass expr:\n    pass\n\n# nameerror: name 'expr' is not defined\n<\/pre>\n<p>\u6211\u4eec\u5728\u5b9a\u4e49\u7c7b expr <em>\u4e4b\u524d<\/em>\u4f7f\u7528\u5b83\u3002\u8fd9\u91cc\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u5b9a\u4e49\u7c7b\u540e\u7b80\u5355\u5730\u8c03\u7528\u88c5\u9970\u5668\uff1a<\/p>\n<pre>class expr:\n    pass\n\nenum(lit=(int,), bin_op=(op, expr, expr))(expr)\n<\/pre>\n<p>\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u6539\u53d8\u6765\u652f\u6301\u8fd9\u4e00\u70b9\uff1a\u5141\u8bb8\u201c\u7b7e\u540d\u201d\u662f\u4e00\u4e2a\u8fd4\u56de\u5143\u7ec4\u7684<em>\u51fd\u6570<\/em>\uff1a<\/p>\n<pre>@enum(lit=(int,), bin_op=lambda: (op, expr, expr))\nclass expr:\n    pass\n<\/pre>\n<p>\u6240\u6709\u8fd9\u4e9b\u90fd\u9700\u8981\u5bf9 make_constructor \u8fdb\u884c\u4e00\u4e9b\u5c0f\u7684\u66f4\u6539\uff1a<\/p>\n<pre>def make_constructor(tag, sig):\n    def constructor(*data):\n        nonlocal sig\n        # if sig is a \"thunk\", thaw it out:\n        if callable(sig):\n            sig = sig()\n\n        # ...\n<\/pre>\n<p>\u5c3d\u7ba1\u6211\u4eec\u7cbe\u7f8e\u7684\u65b0\u679a\u4e3e\u88c5\u9970\u5668\u53ef\u80fd\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4e5f\u6709\u5176\u7f3a\u70b9\u3002\u6700\u660e\u663e\u7684\u662f\u65e0\u6cd5\u6267\u884c\u4efb\u4f55\u7c7b\u578b\u7684\u201c\u5d4c\u5957\u201d\u6a21\u5f0f\u5339\u914d\u3002\u5728 rust \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u505a\u8fd9\u6837\u7684\u4e8b\u60c5\uff1a<\/p>\n<pre>fn foo&lt;t: debug&gt;(x: option&lt;option&lt;t&gt;&gt;) {\n    match x {\n        some(some(value)) =&gt; println!(\"{:?}\", value),\n        _ =&gt; {}\n    }\n}\n<\/pre>\n<p>\u4f46\u662f\u6211\u4eec\u88ab\u8feb\u6267\u884c\u53cc\u91cd\u5339\u914d\u624d\u80fd\u83b7\u5f97\u76f8\u540c\u7684\u7ed3\u679c\uff1a<\/p>\n<pre>def foo(x):\n    return x.match(\n        some=lambda x1: x1.match(\n            some=lambda value: print(value),\n            _=lambda: None\n        ),\n        _=lambda: None\n    )\n<\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u6b64\u7c7b\u6848\u4f8b\u4f3c\u4e4e<em>\u76f8\u5bf9<\/em>\u5f88\u5c11\u89c1\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u7f3a\u70b9\u662f\u5339\u914d\u9700\u8981\u6784\u9020\u548c\u8c03\u7528\u5927\u91cf\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u5b83\u53ef\u80fd\u6bd4\u7b49\u6548\u7684 if-else \u94fe<em>\u6162\u5f97\u591a<\/em>\u3002\u7136\u800c\uff0c\u901a\u5e38\u7684\u7ecf\u9a8c\u6cd5\u5219\u9002\u7528\u4e8e\u6b64\uff1a\u5982\u679c\u60a8\u559c\u6b22\u679a\u4e3e\u7684\u4eba\u4f53\u5de5\u5b66\u4f18\u52bf\uff0c\u8bf7\u4f7f\u7528\u679a\u4e3e\uff1b\u5982\u679c\u5b83\u592a\u6162\uff0c\u5219\u5c06\u5176\u66ff\u6362\u4e3a\u201c\u751f\u6210\u7684\u201d\u4ee3\u7801\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d\u7684\u6c42\u548c\u7c7b\u578b \u5b66\u4e60&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204033","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204033"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204033\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}