{"id":1064407,"date":"2024-12-31T16:08:46","date_gmt":"2024-12-31T08:08:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064407.html"},"modified":"2024-12-31T16:08:48","modified_gmt":"2024-12-31T08:08:48","slug":"%e5%a6%82%e4%bd%95%e7%90%86%e8%a7%a3python%e7%9a%84%e6%8f%8f%e8%bf%b0%e7%ac%a6%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064407.html","title":{"rendered":"\u5982\u4f55\u7406\u89e3python\u7684\u63cf\u8ff0\u7b26\u7c7b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d20fa2f2-9569-40f0-9c22-38466bd60841.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7406\u89e3python\u7684\u63cf\u8ff0\u7b26\u7c7b\" \/><\/p>\n<p><p> <strong>\u7406\u89e3Python\u7684\u63cf\u8ff0\u7b26\u7c7b<\/strong>\u4e3b\u8981\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a\u63cf\u8ff0\u7b26\u534f\u8bae\u3001<code>__get__<\/code>\u65b9\u6cd5\u3001<code>__set__<\/code>\u65b9\u6cd5\u3001<code>__delete__<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u573a\u666f\u3002\u63cf\u8ff0\u7b26\u7c7b\u662f\u5b9e\u73b0\u5c5e\u6027\u7ba1\u7406\u7684\u9ad8\u7ea7\u673a\u5236\uff0c\u80fd\u591f\u66f4\u597d\u5730\u63a7\u5236\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u3002<strong>\u63cf\u8ff0\u7b26\u534f\u8bae\u3001<code>__get__<\/code>\u65b9\u6cd5\u3001<code>__set__<\/code>\u65b9\u6cd5\u3001<code>__delete__<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u63cf\u8ff0\u7b26\u534f\u8bae\u662f\u6307\u7c7b\u4e2d\u5305\u542b<code>__get__<\/code>\u3001<code>__set__<\/code>\u3001<code>__delete__<\/code>\u65b9\u6cd5\u7684\u96c6\u5408\u3002\u901a\u8fc7\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5c5e\u6027\u8bbf\u95ee\u3001\u4fee\u6539\u548c\u5220\u9664\u65f6\u81ea\u5b9a\u4e49\u884c\u4e3a\u3002\u4f8b\u5982\uff0c<code>__get__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fd4\u56de\u503c\uff0c<code>__set__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\uff0c\u800c<code>__delete__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5c5e\u6027\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u5c5e\u6027\u8fdb\u884c\u6709\u6548\u7684\u7ba1\u7406\u548c\u63a7\u5236\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u63cf\u8ff0\u7b26\u534f\u8bae<\/h3>\n<\/p>\n<p><p>\u63cf\u8ff0\u7b26\u534f\u8bae\u662fPython\u4e2d\u7528\u4e8e\u5b9e\u73b0\u63cf\u8ff0\u7b26\u7c7b\u7684\u4e00\u7ec4\u7279\u6b8a\u65b9\u6cd5\uff0c\u5305\u62ec<code>__get__<\/code>\u3001<code>__set__<\/code>\u548c<code>__delete__<\/code>\u3002\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u7684\u7c7b\u53ef\u4ee5\u79f0\u4e3a\u63cf\u8ff0\u7b26\u7c7b\u3002\u63cf\u8ff0\u7b26\u7c7b\u7684\u6838\u5fc3\u529f\u80fd\u5c31\u662f\u901a\u8fc7\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u5bf9\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u8fdb\u884c\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h4>1\u3001<code>__get__<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>__get__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fd4\u56de\u503c\u3002\u5b83\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\uff1aself\u3001instance\u548cowner\u3002\u5176\u4e2d\uff0cself\u8868\u793a\u63cf\u8ff0\u7b26\u5b9e\u4f8b\uff0cinstance\u8868\u793a\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u5b9e\u4f8b\uff0cowner\u8868\u793a\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u7c7b\u3002<code>__get__<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u5c5e\u6027\u7684\u503c\u6216\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        return &#39;value from __get__&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyDescriptor<\/code>\u7c7b\u5b9e\u73b0\u4e86<code>__get__<\/code>\u65b9\u6cd5\uff0c\u5f53\u8bbf\u95ee\u5c5e\u6027\u65f6\u4f1a\u8fd4\u56de\u5b57\u7b26\u4e32&#39;value from <strong>get<\/strong>&#39;\u3002<\/p>\n<\/p>\n<p><h4>2\u3001<code>__set__<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>__set__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u5b83\u63a5\u6536\u4e09\u4e2a\u53c2\u6570\uff1aself\u3001instance\u548cvalue\u3002self\u8868\u793a\u63cf\u8ff0\u7b26\u5b9e\u4f8b\uff0cinstance\u8868\u793a\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u5b9e\u4f8b\uff0cvalue\u8868\u793a\u8981\u8bbe\u7f6e\u7684\u5c5e\u6027\u503c\u3002\u901a\u8fc7<code>__set__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u5c5e\u6027\u503c\u8fdb\u884c\u9a8c\u8bc1\u3001\u8f6c\u6362\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __set__(self, instance, value):<\/p>\n<p>        print(f&#39;Setting attribute to {value}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyDescriptor<\/code>\u7c7b\u5b9e\u73b0\u4e86<code>__set__<\/code>\u65b9\u6cd5\uff0c\u5f53\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u4f1a\u8f93\u51fa\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>3\u3001<code>__delete__<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>__delete__<\/code>\u65b9\u6cd5\u7528\u4e8e\u5220\u9664\u5c5e\u6027\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u5b83\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1aself\u548cinstance\u3002self\u8868\u793a\u63cf\u8ff0\u7b26\u5b9e\u4f8b\uff0cinstance\u8868\u793a\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u5b9e\u4f8b\u3002\u901a\u8fc7<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u5c5e\u6027\u65f6\u6267\u884c\u7279\u5b9a\u7684\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __delete__(self, instance):<\/p>\n<p>        print(&#39;Deleting attribute&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyDescriptor<\/code>\u7c7b\u5b9e\u73b0\u4e86<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u5f53\u5220\u9664\u5c5e\u6027\u65f6\u4f1a\u8f93\u51fa\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001<code>__get__<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>__get__<\/code>\u65b9\u6cd5\u662f\u63cf\u8ff0\u7b26\u534f\u8bae\u4e2d\u7684\u6838\u5fc3\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fd4\u56de\u503c\u3002\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u83b7\u53d6\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbf\u95ee\u5c5e\u6027\u65f6\u7684\u81ea\u5b9a\u4e49\u884c\u4e3a<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u8ba1\u7b97\u5c5e\u6027\u503c\u6216\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u6570\u636e\u3002\u8fd9\u4f7f\u5f97\u5c5e\u6027\u7684\u83b7\u53d6\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u63a7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        if instance is None:<\/p>\n<p>            return self<\/p>\n<p>        return &#39;value from __get__&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u5982\u679c<code>instance<\/code>\u4e3aNone\uff0c\u5219\u8fd4\u56de\u63cf\u8ff0\u7b26\u5b9e\u4f8b\u672c\u8eab\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32&#39;value from <strong>get<\/strong>&#39;\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b9e\u4f8b\u8bbf\u95ee\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>__get__<\/code>\u65b9\u6cd5\u4f1a\u4e0e\u63cf\u8ff0\u7b26\u7c7b\u7684\u5b9e\u4f8b\u4e00\u8d77\u4f7f\u7528\u3002\u901a\u8fc7\u5728\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u7c7b\u4e2d\u5b9a\u4e49\u63cf\u8ff0\u7b26\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u5b9e\u4f8b\u8bbf\u95ee\u5c5e\u6027\u65f6\u89e6\u53d1<code>__get__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    attr = MyDescriptor()<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.attr)  # \u8f93\u51fa\uff1avalue from __get__<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyClass<\/code>\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u63cf\u8ff0\u7b26\u5c5e\u6027<code>attr<\/code>\uff0c\u5f53\u8bbf\u95ee<code>obj.attr<\/code>\u65f6\u4f1a\u89e6\u53d1<code>__get__<\/code>\u65b9\u6cd5\uff0c\u8f93\u51fa&#39;value from <strong>get<\/strong>&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001<code>__set__<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>__set__<\/code>\u65b9\u6cd5\u662f\u63cf\u8ff0\u7b26\u534f\u8bae\u4e2d\u7684\u53e6\u4e00\u4e2a\u6838\u5fc3\u65b9\u6cd5\uff0c\u7528\u4e8e\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u73b0<code>__set__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u8bbe\u7f6e\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__set__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u9a8c\u8bc1\u5c5e\u6027\u503c\u3001\u8f6c\u6362\u5c5e\u6027\u503c\u6216\u5c06\u5c5e\u6027\u503c\u5b58\u50a8\u5728\u7279\u5b9a\u4f4d\u7f6e\u3002\u8fd9\u4f7f\u5f97\u5c5e\u6027\u7684\u8bbe\u7f6e\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u63a7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __set__(self, instance, value):<\/p>\n<p>        if not isinstance(value, str):<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&#39;Value must be a string&#39;)<\/p>\n<p>        instance.__dict__[&#39;attr&#39;] = value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>__set__<\/code>\u65b9\u6cd5\u4f1a\u9a8c\u8bc1\u5c5e\u6027\u503c\u662f\u5426\u4e3a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4e0d\u662f\u5b57\u7b26\u4e32\u5219\u629b\u51fa\u5f02\u5e38\uff1b\u5426\u5219\uff0c\u5c06\u5c5e\u6027\u503c\u5b58\u50a8\u5728\u5b9e\u4f8b\u7684<code>__dict__<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b9e\u4f8b\u8bbe\u7f6e\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>__set__<\/code>\u65b9\u6cd5\u4f1a\u4e0e\u63cf\u8ff0\u7b26\u7c7b\u7684\u5b9e\u4f8b\u4e00\u8d77\u4f7f\u7528\u3002\u901a\u8fc7\u5728\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u7c7b\u4e2d\u5b9a\u4e49\u63cf\u8ff0\u7b26\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u5b9e\u4f8b\u8bbe\u7f6e\u5c5e\u6027\u65f6\u89e6\u53d1<code>__set__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    attr = MyDescriptor()<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.attr = &#39;new value&#39;<\/p>\n<p>print(obj.__dict__)  # \u8f93\u51fa\uff1a{&#39;attr&#39;: &#39;new value&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyClass<\/code>\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u63cf\u8ff0\u7b26\u5c5e\u6027<code>attr<\/code>\uff0c\u5f53\u8bbe\u7f6e<code>obj.attr<\/code>\u65f6\u4f1a\u89e6\u53d1<code>__set__<\/code>\u65b9\u6cd5\uff0c\u5c06\u5c5e\u6027\u503c\u5b58\u50a8\u5728\u5b9e\u4f8b\u7684<code>__dict__<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001<code>__delete__<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><code>__delete__<\/code>\u65b9\u6cd5\u662f\u63cf\u8ff0\u7b26\u534f\u8bae\u4e2d\u7684\u7b2c\u4e09\u4e2a\u6838\u5fc3\u65b9\u6cd5\uff0c\u7528\u4e8e\u5220\u9664\u5c5e\u6027\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9e\u73b0<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5c5e\u6027\u7684\u5220\u9664\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5220\u9664\u5c5e\u6027\u65f6\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5220\u9664\u5c5e\u6027\u65f6\u6267\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u6e05\u7406\u8d44\u6e90\u3001\u5220\u9664\u7f13\u5b58\u6216\u4ece\u6570\u636e\u5e93\u4e2d\u5220\u9664\u6570\u636e\u3002\u8fd9\u4f7f\u5f97\u5c5e\u6027\u7684\u5220\u9664\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u63a7\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyDescriptor:<\/p>\n<p>    def __delete__(self, instance):<\/p>\n<p>        del instance.__dict__[&#39;attr&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>__delete__<\/code>\u65b9\u6cd5\u4f1a\u4ece\u5b9e\u4f8b\u7684<code>__dict__<\/code>\u4e2d\u5220\u9664\u5c5e\u6027<code>attr<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u5b9e\u4f8b\u5220\u9664\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c<code>__delete__<\/code>\u65b9\u6cd5\u4f1a\u4e0e\u63cf\u8ff0\u7b26\u7c7b\u7684\u5b9e\u4f8b\u4e00\u8d77\u4f7f\u7528\u3002\u901a\u8fc7\u5728\u62e5\u6709\u63cf\u8ff0\u7b26\u7684\u7c7b\u4e2d\u5b9a\u4e49\u63cf\u8ff0\u7b26\u5c5e\u6027\uff0c\u53ef\u4ee5\u5728\u5b9e\u4f8b\u5220\u9664\u5c5e\u6027\u65f6\u89e6\u53d1<code>__delete__<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    attr = MyDescriptor()<\/p>\n<p>obj = MyClass()<\/p>\n<p>del obj.attr<\/p>\n<p>print(obj.__dict__)  # \u8f93\u51fa\uff1a{}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>MyClass<\/code>\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u63cf\u8ff0\u7b26\u5c5e\u6027<code>attr<\/code>\uff0c\u5f53\u5220\u9664<code>obj.attr<\/code>\u65f6\u4f1a\u89e6\u53d1<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u4ece\u5b9e\u4f8b\u7684<code>__dict__<\/code>\u4e2d\u5220\u9664\u5c5e\u6027<code>attr<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u63cf\u8ff0\u7b26\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u63cf\u8ff0\u7b26\u7c7b\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u4f7f\u7528\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5bf9\u5c5e\u6027\u8fdb\u884c\u4e25\u683c\u63a7\u5236\u548c\u7ba1\u7406\u7684\u60c5\u51b5\u4e0b\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u63cf\u8ff0\u7b26\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__set__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u8fdb\u884c\u6570\u636e\u9a8c\u8bc1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u5c5e\u6027\u503c\u662f\u5426\u7b26\u5408\u7279\u5b9a\u683c\u5f0f\u3001\u8303\u56f4\u6216\u7c7b\u578b\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u7684\u5408\u6cd5\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class PositiveInteger:<\/p>\n<p>    def __set__(self, instance, value):<\/p>\n<p>        if not isinstance(value, int) or value &lt;= 0:<\/p>\n<p>            raise ValueError(&#39;Value must be a positive integer&#39;)<\/p>\n<p>        instance.__dict__[&#39;attr&#39;] = value<\/p>\n<p>class MyClass:<\/p>\n<p>    attr = PositiveInteger()<\/p>\n<p>obj = MyClass()<\/p>\n<p>obj.attr = 10  # \u8bbe\u7f6e\u5408\u6cd5\u7684\u5c5e\u6027\u503c<\/p>\n<p>print(obj.__dict__)  # \u8f93\u51fa\uff1a{&#39;attr&#39;: 10}<\/p>\n<p>obj.attr = -5  # \u8bbe\u7f6e\u975e\u6cd5\u7684\u5c5e\u6027\u503c\uff0c\u629b\u51fa\u5f02\u5e38<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>PositiveInteger<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5728\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u9a8c\u8bc1\u5176\u662f\u5426\u4e3a\u6b63\u6574\u6570\uff0c\u5982\u679c\u4e0d\u662f\u6b63\u6574\u6570\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c5e\u6027\u7684\u5ef6\u8fdf\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fdb\u884c\u5ef6\u8fdf\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LazyProperty:<\/p>\n<p>    def __init__(self, func):<\/p>\n<p>        self.func = func<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        if instance is None:<\/p>\n<p>            return self<\/p>\n<p>        value = self.func(instance)<\/p>\n<p>        instance.__dict__[self.func.__name__] = value<\/p>\n<p>        return value<\/p>\n<p>class MyClass:<\/p>\n<p>    @LazyProperty<\/p>\n<p>    def expensive_computation(self):<\/p>\n<p>        # \u6a21\u62df\u590d\u6742\u8ba1\u7b97<\/p>\n<p>        return sum(range(1000000))<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.expensive_computation)  # \u7b2c\u4e00\u6b21\u8bbf\u95ee\uff0c\u8fdb\u884c\u8ba1\u7b97\u5e76\u7f13\u5b58\u7ed3\u679c<\/p>\n<p>print(obj.expensive_computation)  # \u7b2c\u4e8c\u6b21\u8bbf\u95ee\uff0c\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>LazyProperty<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u7f13\u5b58\u5230\u5b9e\u4f8b\u7684<code>__dict__<\/code>\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5c5e\u6027\u7684\u53ea\u8bfb\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__set__<\/code>\u548c<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff0c\u907f\u514d\u5c5e\u6027\u503c\u88ab\u4fee\u6539\u6216\u5220\u9664\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ReadOnly:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        return self.value<\/p>\n<p>    def __set__(self, instance, value):<\/p>\n<p>        raise AttributeError(&#39;This attribute is read-only&#39;)<\/p>\n<p>    def __delete__(self, instance):<\/p>\n<p>        raise AttributeError(&#39;This attribute is read-only&#39;)<\/p>\n<p>class MyClass:<\/p>\n<p>    attr = ReadOnly(&#39;initial value&#39;)<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.attr)  # \u8f93\u51fa\uff1ainitial value<\/p>\n<p>obj.attr = &#39;new value&#39;  # \u5c1d\u8bd5\u4fee\u6539\u5c5e\u6027\u503c\uff0c\u629b\u51fa\u5f02\u5e38<\/p>\n<p>del obj.attr  # \u5c1d\u8bd5\u5220\u9664\u5c5e\u6027\uff0c\u629b\u51fa\u5f02\u5e38<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>ReadOnly<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5c06\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff0c\u7981\u6b62\u4fee\u6539\u6216\u5220\u9664\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u52a8\u6001\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u52a8\u6001\u8fd4\u56de\u5c5e\u6027\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u5f53\u524d\u65f6\u95f4\u3001\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u5916\u90e8\u56e0\u7d20\u52a8\u6001\u751f\u6210\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import datetime<\/p>\n<p>class DynamicProperty:<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        return datetime.datetime.now().strftime(&#39;%Y-%m-%d %H:%M:%S&#39;)<\/p>\n<p>class MyClass:<\/p>\n<p>    current_time = DynamicProperty()<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.current_time)  # \u8f93\u51fa\u5f53\u524d\u65f6\u95f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>DynamicProperty<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5728\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u63cf\u8ff0\u7b26\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\uff0c\u63cf\u8ff0\u7b26\u7c7b\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u5c5e\u6027\u4ee3\u7406\u3001\u5c5e\u6027\u7f13\u5b58\u3001\u5c5e\u6027\u7684\u591a\u6001\u884c\u4e3a\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5c5e\u6027\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u3001<code>__set__<\/code>\u548c<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u4ee3\u7406\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e0a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u4ee3\u7406\u5230\u6570\u636e\u5e93\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u5916\u90e8\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Proxy:<\/p>\n<p>    def __init__(self, target):<\/p>\n<p>        self.target = target<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        return getattr(self.target, &#39;attr&#39;)<\/p>\n<p>    def __set__(self, instance, value):<\/p>\n<p>        setattr(self.target, &#39;attr&#39;, value)<\/p>\n<p>    def __delete__(self, instance):<\/p>\n<p>        delattr(self.target, &#39;attr&#39;)<\/p>\n<p>class Target:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.attr = &#39;initial value&#39;<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.target = Target()<\/p>\n<p>        self.proxy = Proxy(self.target)<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.proxy)  # \u8f93\u51fa\uff1ainitial value<\/p>\n<p>obj.proxy = &#39;new value&#39;<\/p>\n<p>print(obj.target.attr)  # \u8f93\u51fa\uff1anew value<\/p>\n<p>del obj.proxy<\/p>\n<p>print(hasattr(obj.target, &#39;attr&#39;))  # \u8f93\u51fa\uff1aFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>Proxy<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5c06\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u4ee3\u7406\u5230\u76ee\u6807\u5bf9\u8c61<code>target<\/code>\u4e0a\uff0c\u5b9e\u73b0\u5c5e\u6027\u4ee3\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5c5e\u6027\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u5c5e\u6027\u503c\u7f13\u5b58\u8d77\u6765\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u7f13\u5b58\u8d77\u6765\uff0c\u540e\u7eed\u8bbf\u95ee\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class CachedProperty:<\/p>\n<p>    def __init__(self, func):<\/p>\n<p>        self.func = func<\/p>\n<p>        self.cache_name = f&#39;_{func.__name__}_cache&#39;<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        if instance is None:<\/p>\n<p>            return self<\/p>\n<p>        if not hasattr(instance, self.cache_name):<\/p>\n<p>            value = self.func(instance)<\/p>\n<p>            setattr(instance, self.cache_name, value)<\/p>\n<p>        return getattr(instance, self.cache_name)<\/p>\n<p>class MyClass:<\/p>\n<p>    @CachedProperty<\/p>\n<p>    def expensive_computation(self):<\/p>\n<p>        # \u6a21\u62df\u590d\u6742\u8ba1\u7b97<\/p>\n<p>        return sum(range(1000000))<\/p>\n<p>obj = MyClass()<\/p>\n<p>print(obj.expensive_computation)  # \u7b2c\u4e00\u6b21\u8bbf\u95ee\uff0c\u8fdb\u884c\u8ba1\u7b97\u5e76\u7f13\u5b58\u7ed3\u679c<\/p>\n<p>print(obj.expensive_computation)  # \u7b2c\u4e8c\u6b21\u8bbf\u95ee\uff0c\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>CachedProperty<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u5c5e\u6027\u65f6\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u7f13\u5b58\u5230\u5b9e\u4f8b\u7684\u5c5e\u6027\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5c5e\u6027\u7684\u591a\u6001\u884c\u4e3a<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9e\u73b0<code>__get__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u8fd4\u56de\u4e0d\u540c\u7684\u5c5e\u6027\u503c\uff0c\u5b9e\u73b0\u5c5e\u6027\u7684\u591a\u6001\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u89d2\u8272\u3001\u8bbe\u5907\u7c7b\u578b\u6216\u5176\u4ed6\u6761\u4ef6\u8fd4\u56de\u4e0d\u540c\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class PolymorphicProperty:<\/p>\n<p>    def __init__(self, func):<\/p>\n<p>        self.func = func<\/p>\n<p>    def __get__(self, instance, owner):<\/p>\n<p>        if instance is None:<\/p>\n<p>            return self<\/p>\n<p>        return self.func(instance)<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, role):<\/p>\n<p>        self.role = role<\/p>\n<p>    @PolymorphicProperty<\/p>\n<p>    def greeting(self):<\/p>\n<p>        if self.role == &#39;admin&#39;:<\/p>\n<p>            return &#39;Hello, admin!&#39;<\/p>\n<p>        elif self.role == &#39;user&#39;:<\/p>\n<p>            return &#39;Hello, user!&#39;<\/p>\n<p>        else:<\/p>\n<p>            return &#39;Hello, guest!&#39;<\/p>\n<p>admin = MyClass(&#39;admin&#39;)<\/p>\n<p>user = MyClass(&#39;user&#39;)<\/p>\n<p>guest = MyClass(&#39;guest&#39;)<\/p>\n<p>print(admin.greeting)  # \u8f93\u51fa\uff1aHello, admin!<\/p>\n<p>print(user.greeting)  # \u8f93\u51fa\uff1aHello, user!<\/p>\n<p>print(guest.greeting)  # \u8f93\u51fa\uff1aHello, guest!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>PolymorphicProperty<\/code>\u63cf\u8ff0\u7b26\u7c7b\u4f1a\u6839\u636e\u5b9e\u4f8b\u7684<code>role<\/code>\u5c5e\u6027\u8fd4\u56de\u4e0d\u540c\u7684\u95ee\u5019\u8bed\uff0c\u5b9e\u73b0\u5c5e\u6027\u7684\u591a\u6001\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u63cf\u8ff0\u7b26\u7c7b\u662fPython\u4e2d\u5b9e\u73b0\u5c5e\u6027\u7ba1\u7406\u7684\u9ad8\u7ea7\u673a\u5236\uff0c\u901a\u8fc7\u5b9e\u73b0\u63cf\u8ff0\u7b26\u534f\u8bae\u4e2d\u7684<code>__get__<\/code>\u3001<code>__set__<\/code>\u548c<code>__delete__<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u5c5e\u6027\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u8fdb\u884c\u63a7\u5236\u3002\u63cf\u8ff0\u7b26\u7c7b\u5728\u6570\u636e\u9a8c\u8bc1\u3001\u5c5e\u6027\u7684\u5ef6\u8fdf\u8ba1\u7b97\u3001\u5c5e\u6027\u7684\u53ea\u8bfb\u63a7\u5236\u3001\u52a8\u6001\u5c5e\u6027\u3001\u5c5e\u6027\u4ee3\u7406\u3001\u5c5e\u6027\u7f13\u5b58\u548c\u5c5e\u6027\u7684\u591a\u6001\u884c\u4e3a\u7b49\u65b9\u9762\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u63cf\u8ff0\u7b26\u7c7b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5b9e\u73b0\u66f4\u7075\u6d3b\u548c\u53ef\u63a7\u7684\u5c5e\u6027\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u63cf\u8ff0\u7b26\u7c7b\u7684\u5b9a\u4e49\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u63cf\u8ff0\u7b26\u7c7b\u662fPython\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u5bf9\u8c61\uff0c\u5b83\u901a\u8fc7\u5b9a\u4e49<code>__get__<\/code>\u3001<code>__set__<\/code>\u548c<code>__delete__<\/code>\u65b9\u6cd5\u6765\u7ba1\u7406\u5bf9\u67d0\u4e2a\u5c5e\u6027\u7684\u8bbf\u95ee\u3002\u63cf\u8ff0\u7b26\u53ef\u4ee5\u63a7\u5236\u5c5e\u6027\u7684\u83b7\u53d6\u3001\u8bbe\u7f6e\u548c\u5220\u9664\u884c\u4e3a\uff0c\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u5c5e\u6027\u7684\u8bbf\u95ee\u63a7\u5236\u6216\u6570\u636e\u9a8c\u8bc1\u3002\u901a\u8fc7\u4f7f\u7528\u63cf\u8ff0\u7b26\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u7684\u5c5e\u6027\u903b\u8f91\uff0c\u4ece\u800c\u589e\u5f3a\u7c7b\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u8be5\u4f7f\u7528\u63cf\u8ff0\u7b26\u7c7b\uff1f<\/strong><br \/>\u63cf\u8ff0\u7b26\u7c7b\u975e\u5e38\u9002\u5408\u9700\u8981\u5728\u5c5e\u6027\u8bbf\u95ee\u65f6\u6dfb\u52a0\u989d\u5916\u903b\u8f91\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5f53\u9700\u8981\u5bf9\u5c5e\u6027\u503c\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u3001\u8303\u56f4\u9650\u5236\u6216\u8005\u5ef6\u8fdf\u8ba1\u7b97\u65f6\uff0c\u63cf\u8ff0\u7b26\u53ef\u4ee5\u63d0\u4f9b\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u6b64\u5916\uff0c\u63cf\u8ff0\u7b26\u5728\u5b9e\u73b0ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u6216\u6570\u636e\u6a21\u578b\u65f6\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u7684\u83b7\u53d6\u548c\u4fdd\u5b58\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u5b9e\u73b0\u63cf\u8ff0\u7b26\uff1f<\/strong><br \/>\u8981\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u5b9e\u73b0\u63cf\u8ff0\u7b26\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u63cf\u8ff0\u7b26\u7c7b\uff0c\u5e76\u5b9e\u73b0<code>__get__<\/code>\u3001<code>__set__<\/code>\u548c<code>__delete__<\/code>\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u5728\u76ee\u6807\u7c7b\u4e2d\u5c06\u63cf\u8ff0\u7b26\u5b9e\u4f8b\u4f5c\u4e3a\u5c5e\u6027\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u8bbf\u95ee\u8be5\u5c5e\u6027\u65f6\u4f1a\u89e6\u53d1\u63cf\u8ff0\u7b26\u7684\u76f8\u5e94\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u9a8c\u8bc1\u5e74\u9f84\u7684\u63cf\u8ff0\u7b26\uff0c\u53ef\u4ee5\u5728<code>__set__<\/code>\u65b9\u6cd5\u4e2d\u68c0\u67e5\u8f93\u5165\u7684\u5e74\u9f84\u662f\u5426\u5728\u5408\u7406\u8303\u56f4\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7406\u89e3Python\u7684\u63cf\u8ff0\u7b26\u7c7b\u4e3b\u8981\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\uff1a\u63cf\u8ff0\u7b26\u534f\u8bae\u3001__get__\u65b9\u6cd5\u3001__set__\u65b9\u6cd5\u3001__ [&hellip;]","protected":false},"author":3,"featured_media":1064416,"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\/1064407"}],"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=1064407"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064407\/revisions"}],"predecessor-version":[{"id":1064417,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064407\/revisions\/1064417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064416"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}