{"id":1183443,"date":"2025-01-15T19:14:42","date_gmt":"2025-01-15T11:14:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183443.html"},"modified":"2025-01-15T19:14:45","modified_gmt":"2025-01-15T11:14:45","slug":"python%e5%b1%9e%e6%80%a7%e5%a6%82%e4%bd%95%e7%94%a8%e5%8f%98%e9%87%8f%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183443.html","title":{"rendered":"python\u5c5e\u6027\u5982\u4f55\u7528\u53d8\u91cf\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133221\/bc274f27-9158-4940-8e69-b64b7214ae34.webp\" alt=\"python\u5c5e\u6027\u5982\u4f55\u7528\u53d8\u91cf\u5b9e\u73b0\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u52a8\u6001\u5730\u8bbf\u95ee\u6216\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>getattr<\/code>\u548c<code>setattr<\/code>\u3002<strong>\u4f7f\u7528<code>getattr<\/code>\u3001<code>setattr<\/code>\u548c<code>__dict__<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u901a\u8fc7\u53d8\u91cf\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u5c5e\u6027<\/strong>\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u63cf\u8ff0\u548c\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>getattr<\/code>\u548c<code>setattr<\/code>\u5b9e\u73b0\u52a8\u6001\u5c5e\u6027\u8bbf\u95ee<\/h3>\n<\/p>\n<p><h4>1\u3001<code>getattr<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>getattr<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u5bf9\u8c61\u548c\u5c5e\u6027\u540d\uff08\u4f5c\u4e3a\u5b57\u7b26\u4e32\uff09\u3002\u5982\u679c\u5c5e\u6027\u5b58\u5728\uff0c\u5b83\u5c06\u8fd4\u56de\u76f8\u5e94\u7684\u5c5e\u6027\u503c\uff1b\u5982\u679c\u5c5e\u6027\u4e0d\u5b58\u5728\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.my_attribute = value<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>attr_name = &#39;my_attribute&#39;<\/p>\n<h2><strong>\u4f7f\u7528getattr\u83b7\u53d6\u5c5e\u6027\u503c<\/strong><\/h2>\n<p>attribute_value = getattr(obj, attr_name)<\/p>\n<p>print(attribute_value)  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<code>setattr<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>setattr<\/code>\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\u5b83\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\uff1a\u5bf9\u8c61\u3001\u5c5e\u6027\u540d\uff08\u4f5c\u4e3a\u5b57\u7b26\u4e32\uff09\u548c\u65b0\u503c\u3002\u5982\u679c\u5c5e\u6027\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u521b\u5efa\u8be5\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.my_attribute = value<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>attr_name = &#39;my_attribute&#39;<\/p>\n<h2><strong>\u4f7f\u7528setattr\u8bbe\u7f6e\u5c5e\u6027\u503c<\/strong><\/h2>\n<p>setattr(obj, attr_name, 20)<\/p>\n<p>print(obj.my_attribute)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>__dict__<\/code>\u5c5e\u6027\u5b57\u5178<\/h3>\n<\/p>\n<p><h4>1\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5c5e\u6027<code>__dict__<\/code>\uff0c\u5b83\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e86\u5bf9\u8c61\u7684\u6240\u6709\u53ef\u53d8\u5c5e\u6027\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u8fd9\u4e2a\u5b57\u5178\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.my_attribute = value<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>attr_name = &#39;my_attribute&#39;<\/p>\n<h2><strong>\u901a\u8fc7__dict__\u8bbf\u95ee\u5c5e\u6027\u503c<\/strong><\/h2>\n<p>attribute_value = obj.__dict__[attr_name]<\/p>\n<p>print(attribute_value)  # \u8f93\u51fa: 10<\/p>\n<h2><strong>\u901a\u8fc7__dict__\u4fee\u6539\u5c5e\u6027\u503c<\/strong><\/h2>\n<p>obj.__dict__[attr_name] = 30<\/p>\n<p>print(obj.my_attribute)  # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6dfb\u52a0\u65b0\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u76f4\u63a5\u64cd\u4f5c<code>__dict__<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4e3a\u5bf9\u8c61\u52a8\u6001\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.my_attribute = value<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>new_attr_name = &#39;new_attribute&#39;<\/p>\n<h2><strong>\u901a\u8fc7__dict__\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong><\/h2>\n<p>obj.__dict__[new_attr_name] = 40<\/p>\n<p>print(obj.new_attribute)  # \u8f93\u51fa: 40<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u4ee5\u4e0a\u65b9\u6cd5\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u52a8\u6001\u5c5e\u6027\u8bbf\u95ee\u548c\u4fee\u6539\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u52a8\u6001\u6570\u636e\u7ed3\u6784\u6216\u9700\u8981\u6839\u636e\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u5bf9\u8c61\u5c5e\u6027\u65f6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u52a8\u6001\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u52a8\u6001\u6570\u636e\u7ed3\u6784\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\u90fd\u662f\u52a8\u6001\u7684\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>getattr<\/code>\u548c<code>setattr<\/code>\u6765\u5904\u7406\u8fd9\u4e9b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class DynamicData:<\/p>\n<p>    def __init__(self, data_dict):<\/p>\n<p>        for key, value in data_dict.items():<\/p>\n<p>            setattr(self, key, value)<\/p>\n<p>data = {&#39;attribute1&#39;: 100, &#39;attribute2&#39;: 200}<\/p>\n<p>dynamic_obj = DynamicData(data)<\/p>\n<p>print(dynamic_obj.attribute1)  # \u8f93\u51fa: 100<\/p>\n<p>print(dynamic_obj.attribute2)  # \u8f93\u51fa: 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6839\u636e\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Config:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.debug = False<\/p>\n<p>        self.verbose = False<\/p>\n<p>config = Config()<\/p>\n<p>condition = &#39;debug_mode&#39;<\/p>\n<p>if condition == &#39;debug_mode&#39;:<\/p>\n<p>    setattr(config, &#39;debug&#39;, True)<\/p>\n<p>print(config.debug)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>getattr<\/code>\u3001<code>setattr<\/code>\u548c<code>__dict__<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u5728Python\u4e2d\u5b9e\u73b0\u52a8\u6001\u5c5e\u6027\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u548c\u9700\u8981\u6839\u636e\u6761\u4ef6\u52a8\u6001\u8c03\u6574\u5bf9\u8c61\u5c5e\u6027\u65f6\u5c24\u5176\u6709\u7528\u3002<strong><code>getattr<\/code>\u51fd\u6570\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\uff0c<code>setattr<\/code>\u51fd\u6570\u7528\u4e8e\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\uff0c\u800c<code>__dict__<\/code>\u5c5e\u6027\u5b57\u5178\u5219\u63d0\u4f9b\u4e86\u76f4\u63a5\u64cd\u4f5c\u5bf9\u8c61\u5c5e\u6027\u7684\u9014\u5f84<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u66f4\u52a0\u52a8\u6001\u548c\u7075\u6d3b\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a8\u6001\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>setattr()<\/code>\u51fd\u6570\u6765\u52a8\u6001\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u901a\u8fc7\u4f20\u5165\u5bf9\u8c61\u3001\u5c5e\u6027\u540d\uff08\u5b57\u7b26\u4e32\u5f62\u5f0f\uff09\u548c\u5c5e\u6027\u503c\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6dfb\u52a0\u6216\u4fee\u6539\u5c5e\u6027\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    pass\n\nobj = MyClass()\nsetattr(obj, &#39;new_attr&#39;, 42)\nprint(obj.new_attr)  # \u8f93\u51fa: 42\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u5408\u9700\u8981\u52a8\u6001\u7ba1\u7406\u5c5e\u6027\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u4f7f\u7528\u5b57\u5178\u6765\u7ba1\u7406Python\u5bf9\u8c61\u7684\u5c5e\u6027\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u5c5e\u6027\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u901a\u8fc7\u5c06\u5bf9\u8c61\u7684\u5c5e\u6027\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u5c5e\u6027\uff0c\u800c\u4e0d\u9700\u8981\u5728\u7c7b\u5b9a\u4e49\u4e2d\u663e\u5f0f\u5730\u58f0\u660e\u5b83\u4eec\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self):\n        self.attributes = {}\n\nobj = MyClass()\nobj.attributes[&#39;dynamic_attr&#39;] = &#39;Hello&#39;\nprint(obj.attributes[&#39;dynamic_attr&#39;])  # \u8f93\u51fa: Hello\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u5c5e\u6027\u6570\u91cf\u4e0d\u786e\u5b9a\u6216\u9700\u8981\u6839\u636e\u5916\u90e8\u6570\u636e\u52a8\u6001\u53d8\u5316\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528<code>property<\/code>\u88c5\u9970\u5668\u5b9e\u73b0\u52a8\u6001\u5c5e\u6027\uff1f<\/strong><br \/><code>property<\/code>\u88c5\u9970\u5668\u5141\u8bb8\u4f60\u5c06\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5c5e\u6027\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u6267\u884c\u7279\u5b9a\u7684\u903b\u8f91\u3002\u901a\u8fc7\u7ed3\u5408<code>property<\/code>\u548c\u53d8\u91cf\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a8\u6001\u8ba1\u7b97\u5c5e\u6027\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def __init__(self, value):\n        self._value = value\n\n    @property\n    def dynamic_property(self):\n        return self._value * 2\n\nobj = MyClass(10)\nprint(obj.dynamic_property)  # \u8f93\u51fa: 20\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u52a8\u6001\u8ba1\u7b97\u7684\u80fd\u529b\uff0c\u8fd8\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u8fdb\u884c\u66f4\u590d\u6742\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u52a8\u6001\u5730\u8bbf\u95ee\u6216\u8bbe\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570getattr\u548csetattr\u3002\u4f7f\u7528ge [&hellip;]","protected":false},"author":3,"featured_media":1183447,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183443"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1183443"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183443\/revisions"}],"predecessor-version":[{"id":1183450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183443\/revisions\/1183450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183447"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}