{"id":549,"date":"2023-08-21T19:46:09","date_gmt":"2023-08-21T11:46:09","guid":{"rendered":"https:\/\/himpqblog.cn\/?p=549"},"modified":"2023-08-21T19:59:40","modified_gmt":"2023-08-21T11:59:40","slug":"python-%e4%b8%87%e7%89%a9%e7%9a%86%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/himpqblog.cn\/index.php\/archives\/549","title":{"rendered":"Python \u4e07\u7269\u7686\u5bf9\u8c61"},"content":{"rendered":"\n<p>\u5728python\u4e2d\uff0c\u5c0f\u5230\u57fa\u7840\u7c7b\u578b\uff0c\u5927\u5230\u5185\u7f6e\u65b9\u6cd5\u51fd\u6570\u3001\u7c7b\u7b49\u5168\u90fd\u662f\u5bf9\u8c61\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyObj: ...\n\nprint(type(MyObj))   #&lt;class &#039;type&#039;&gt;\nprint(type(MyObj())) #&lt;class &#039;__main__.MyObj&#039;&gt;\n<\/pre><\/div>\n\n\n<p>\u5c31\u8fde<strong><em>type<\/em><\/strong>\u7684\u63cf\u8ff0\u5b9e\u9645\u4e0a\u4e5f\u662f\u4e00\u4e2a\u7c7b\uff08<em><strong>builtins.pyi<\/strong><\/em>\u53ef\u67e5\uff09<\/p>\n\n\n\n<p>\u56e0\u6b64\u4fbf\u8bde\u751f\u4e86\u8bb8\u591a\u9b54\u672f\u65b9\u6cd5\uff0c\u80fd\u591f\u5168\u9762\u7684\u64cd\u7eb5\u8be5\u5bf9\u8c61\u7684\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __repr__(self):\n        return &quot;&lt;MyClass object&gt;&quot;\n    def __str__(self):\n        return &quot;This is a class&quot;\n\nprint(MyClass()) #&quot;This is a class&quot;\nMyClass()        #&lt;MyClass Object&gt;\n<\/pre><\/div>\n\n\n<p>\u4e0a\u9762\u4e00\u6bb5\u4ee3\u7801\u66f4\u6539\u4e86<strong><em>MyClass<\/em><\/strong>\u5728\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u65f6\u7684\u64cd\u4f5c\u3002\u5176\u4e2d<strong><em>__str__<\/em><\/strong>\u65b9\u6cd5\u662f\u5f53\u76f4\u63a5\u8c03\u7528str()\u8fdb\u884c\u5b57\u7b26\u4e32\u5316\u65f6\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\uff08\u5927\u591a\u4e3aprint\u7b49\u51fd\u6570\uff09\uff0c<em><strong>__repr__<\/strong><\/em>\u65b9\u6cd5\u5219\u662f\u5728\u8c03\u8bd5\u65f6\u8fd4\u56de\u7684\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u5982\u8be5\u5bf9\u8c61\u5728\u4e00\u4e2a\u5217\u8868\u91cc\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmylist = &#x5B;MyClass(), MyClass()]\nprint(mylist) #&#x5B;&lt;MyClass object&gt;, &lt;MyClass object&gt;]\n<\/pre><\/div>\n\n\n<p>\u6b64\u65f6\u4fbf\u4f1a\u8c03\u7528<strong><em>__repr__<\/em><\/strong>\u8fd4\u56de\u66f4\u8be6\u7ec6\u7684\u5185\u5bb9\uff0c\u5f53\u7136\u8fd9\u4e9b\u5185\u5bb9\u5728\u91cd\u5199\u9b54\u672f\u65b9\u6cd5\u65f6\u8fd4\u56de\u7684\u6570\u636e\u4f9d\u4e8e\u4f60\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u9b54\u672f\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u505a\u5230\u91cd\u5199\u8fd0\u7b97\u7b26\u7b49\u64cd\u4f5c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass MyClass:\n    def __repr__(self):\n        return &quot;&lt;MyClass object&gt;&quot;\n    def __str__(self):\n        return &quot;This is a class&quot;\n    def __eq__(self, a):\n        return True\n    def __getitem__(self, key):\n        return self.__dict__&#x5B;key]\n<\/pre><\/div>\n\n\n<p><strong><em>__eq__<\/em><\/strong>\u65b9\u6cd5\u91cd\u5199\u4e86\u5f53<strong><em>MyClass<\/em><\/strong>\u5bf9\u8c61\u88ab\u62ff\u53bb\u505a\u76f8\u7b49\u8fd0\u7b97\u65f6\u7684\u64cd\u4f5c\uff0c<strong><em>__getitem__<\/em><\/strong>\u91cd\u5199\u4e86\u5f53\u5bf9\u8c61\u88ab\u50cf\u5b57\u5178\u4e00\u6837\u901a\u8fc7\u201c<strong><em>obj[index]<\/em><\/strong>\u201d\u8fdb\u884c\u53d6\u503c\u64cd\u4f5c\u65f6\u8fd4\u56de\u7684\u6570\u636e\uff0c\u7c7b\u4f3c\u4e8ec++\u4e2d\u7684<em><strong>operator<\/strong><\/em>\u3002<\/p>\n\n\n\n<p><strong><em>__dict__<\/em><\/strong>\u5728\u5bf9\u8c61\u4e2d\u662f\u4e00\u4e2a\u6bd4\u8f83\u7279\u6b8a\u7684\u9b54\u6cd5\u53d8\u91cf\uff0c\u5b83\u5b58\u50a8\u7740\u5f53\u524d\u5bf9\u8c61\u6240\u6709\u6210\u5458\u53d8\u91cf\u3002\u901a\u5e38\u4ed6\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5728\u4e00\u4e9b\u5185\u7f6e\u7c7b\u4e2d\u5b83\u5219\u662f\u4e00\u4e2a<em><strong>mappingproxy<\/strong><\/em> \uff08\u56e0\u4e3a\u5927\u591a\u5185\u7f6e\u7c7b\u7531C\u5b9e\u73b0\uff09\u3002<\/p>\n\n\n\n<p>\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\u90fd\u6709<strong><em>object<\/em><\/strong>\u7c7b\u3002<\/p>\n\n\n\n<p>\u4e43\u81f3\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef myFunc(): ...\n\nmyFunc.__qualname__ = &quot;123&quot;\nprint(myFunc) #&lt;function 123 at ...&gt;\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>python\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5f3a\u7c7b\u578b\u7684\u8bed\u8a00\uff0c\u4f60\u5f53\u7136\u4e0d\u53ef\u5c1d\u8bd5\u5b57\u7b26\u4e32+\u6570\u5b57\u8fd9\u79cd\u8fd0\u7b97\uff08js\u5374\u53ef\u4ee5\uff09\uff0c\u56e0\u4e3a\u57fa\u672c\u7c7b\u578b\u7684\u8fd0\u7b97\u65b9\u6cd5\u662f\u5199\u6b7b\u7684\uff0c\u4f60\u53ea\u80fd\u7ee7\u627f\u57fa\u672c\u7c7b\u578b\u91cd\u5199\u8be5\u9b54\u672f\u65b9\u6cd5\u6765\u5b9e\u73b0\u8ddf\u57fa\u672c\u7c7b\u578b\u51e0\u4e4e\u4e00\u6837\u7684\u4e00\u4e2a\u7c7b\u3002<\/p>\n\n\n\n<p>\u6240\u6709\u53d8\u91cf\u7684\u7c7b\u578b\u5728\u5b9a\u4e49\u7684\u90a3\u4e00\u523b\u5c31\u5df2\u7ecf\u786e\u5b9a\uff0c\u4e14\u5e76\u6ca1\u6709\u58f0\u660e\u7684\u64cd\u4f5c\uff0c\u4f60\u80fd\u505a\u7684\u9876\u591a\u5c31\u662f<strong><em> myVar : str = None<\/em><\/strong> \u5982\u6b64\u58f0\u660e\uff0c\u4f46\u5e76\u4e0d\u4f1a\u6709\u4efb\u4f55\u7684\u6548\u679c\uff0c\u53ea\u662fIDE\u4f1a\u5c06\u8be5\u53d8\u91cf\u5f53\u4f5c<strong><em>str<\/em><\/strong>\u7c7b\u578b\u7ed9\u4f60\u63d0\u4f9b\u4ed6\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Python\u4e07\u7269\u7686\u5bf9\u8c61\u7684\u6982\u5ff5\u52a0\u4e0a\u4ea4\u4e92\u5f0f\u6267\u884c\uff0c\u80fd\u8ba9python\u5f88\u597d\u7684debug\uff0c\u800c\u65e0\u9700\u50cfC++\u7b49\u9700\u8981\u7f16\u8bd1\u7684\u8bed\u8a00\u4e00\u6837\u5355\u72ec\u62c9\u51fa\u4e00\u4e2a\u6587\u4ef6\u6765\u91cd\u65b0\u6d4b\u8bd5\u3002\u4f46\u662f\u89e3\u91ca\u5668\u4f7f\u5f97python\u7684\u901f\u5ea6\u6162\u4e0a\u4e00\u5927\u622a\uff0cCPython\u7684GIL\u9501\u867d\u7136\u4f18\u5316\u4e86\u5355\u6838\u6027\u80fd\u4f46\u662f\u591a\u7ebf\u7a0b\u662f\u5047\u591a\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u5c3d\u7ba1python\u4e2d\u6ca1\u6709<strong><em>interface<\/em><\/strong>\u8fd9\u4e00\u5173\u952e\u5b57\uff0c\u4f46\u662f\u4ecd\u53ef\u4ee5\u901a\u8fc7<strong><em>abc<\/em><\/strong>\u6a21\u5757\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u4f3c\u4e8e\u63a5\u53e3\u7684\u7c7b\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport abc\n\nclass Interface(metaclass=abc.ABCMeta):\n    @abc.abstractmethod\n    def test(self): ...\n    @abc.abstractmethod\n    def print(self): ...\n<\/pre><\/div>\n\n\n<p>\u800c\u7ee7\u627f\u5b83\u7684\u5b50\u7c7b\u5fc5\u987b\u5b9e\u73b0 <strong><em>test<\/em><\/strong>\u3001<strong><em>print <\/em><\/strong>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5426\u5219\u65e0\u6cd5\u5b9e\u4f8b\u5316\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass myClass(Interface): ...\n\na = myClass()  \n# TypeError: Can&#039;t instantiate abstract class r with abstract methods print, test\n<\/pre><\/div>\n\n\n<p>\u5b9a\u4e49\u63a5\u53e3\u65f6\u5728\u7c7b\u540d\u58f0\u660e\u90e8\u5206\u6709\u4e00\u4e2a\u6bd4\u8f83\u523a\u773c\u7684\u5730\u65b9\uff1a <strong><em>(metaclass=abc.ABCMeta)<\/em><\/strong><\/p>\n\n\n\n<p>\u5b83\u4e0e\u53e6\u4e00\u4e2a\u9b54\u672f\u65b9\u6cd5\u76f8\u5173\u8054\uff1a<strong><em>__init_subclass__<\/em><\/strong>\u3002\u5728 <a href=\"https:\/\/peps.python.org\/pep-0487\/\">PEP 487<\/a> \u4e2d\u6709\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u4e0b\u9762\u4e3e\u51fa\u5176\u4e2d\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; class QuestBase:\n...    # this is implicitly a @classmethod (see below for motivation)\n...    def __init_subclass__(cls, swallow, **kwargs):\n...        cls.swallow = swallow\n...        super().__init_subclass__(**kwargs)\n\n&gt;&gt;&gt; class Quest(QuestBase, swallow=&quot;african&quot;):\n...    pass\n\n&gt;&gt;&gt; Quest.swallow\n&#039;african&#039;\n<\/pre><\/div>\n\n\n<p>\u53ef\u89c1\uff0c\u5728 <strong><em>QuestBase <\/em><\/strong>\u7c7b\u4e2d\u5b9a\u4e49\u4e86 <strong><em>__init_subclass__ <\/em><\/strong>\u8fd9\u4e00\u9b54\u672f\u65b9\u6cd5\uff0c\u5728 <strong><em>Quest <\/em><\/strong>\u7ee7\u627f\u4e86\u7c7b <strong><em>QuestBase<\/em><\/strong>\uff0c\u518d\u901a\u8fc7 <strong><em>swallow <\/em><\/strong>\u4f20\u9012\u4e86 <strong><em>&#8220;african&#8221;<\/em><\/strong> \u8fd9\u4e00\u53c2\u6570\u3002\u6700\u540e\u518d\u5728 QuestBase \u4e2d\u7684 <strong><em>__init_subclass__ <\/em><\/strong>\u5b9a\u4e49\u7ed9\u4e86 <strong><em>Quest<\/em><\/strong> \u7c7b\u4e00\u4e2a <strong><em>swallow <\/em><\/strong>\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u5b9a\u4e49\u7ed9\u4e86 <strong><em>Quest <\/em><\/strong>\u7c7b\u4e00\u4e2a <strong><em>swallow <\/em><\/strong>\uff0c\u8be5\u6210\u5458\u53d8\u91cf\u4f1a\u5728\u4efb\u4f55 <strong><em>Quest <\/em><\/strong>\u7684\u5b9e\u4f8b\u5316\u4e2d\u51fa\u73b0\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass a: ...\n\na.test = 1\n\nt1, t2, t3 = a(), a(), a()\n\nprint(t1.test, t2.test, t3.test) # 1 1 1\n<\/pre><\/div>\n\n\n<p>\u5173\u4e8eabc\u6a21\u5757\u4e0e\u5143\u7c7b\u8fd9\u65b9\u9762\u53ef\u4ee5\u641c\u7d22\u5143\u7f16\u7a0b\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8c08\u5230\u5b50\u7c7b\u7236\u7c7b\uff0c\u4e0d\u5f97\u4e0d\u63d0\u53ca <strong><em>public<\/em><\/strong>\u3001<strong><em>protected<\/em><\/strong>\u3001<strong><em>private<\/em><\/strong> \u8fd9\u4e09\u4e2a\u5bb6\u4f19\u3002<\/p>\n\n\n\n<p>Python\u4e2d\u5e76\u6ca1\u6709\u8fd9\u4e09\u4e2a\u5173\u952e\u5b57\uff0c\u901a\u5e38\u4f7f\u7528\u7ea6\u5b9a\u4fd7\u6210\u7684 \u201c_\u201d \u4e0b\u5212\u7ebf\u5b9a\u4e49\u53d8\u91cf\u540d\u6765\u8bf4\u660e\u8fd9\u662f\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4f46\u8fd9\u4e5f\u4ec5\u9650\u4e8e\u7ea6\u5b9a\u4fd7\u6210\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass a:\n    def __init__(self):\n        self.__p = 1\n        self._p  = 2\n\nclass b(a):\n    def __init__(self):\n        super().__init__()\n\nt = b()\nprint(t._a__p, t._p)  #  1 2\n<\/pre><\/div>\n\n\n<p>\u53ef\u89c1\uff0c\u901a\u5e38\u4f7f\u7528<em><strong>\u53cc\u4e0b\u5212\u7ebf+\u53d8\u91cf\u540d<\/strong><\/em>\u7684\u65b9\u6cd5\u8bf4\u660e\u8fd9\u662f <strong><em>private<\/em><\/strong> \u53d8\u91cf\uff0c\u4f7f\u7528<strong><em>\u5355\u4e0b\u5212\u7ebf+\u53d8\u91cf\u540d<\/em><\/strong>\u8bf4\u660e\u8fd9\u662f <em><strong>protected <\/strong><\/em>\u53d8\u91cf\uff0c\u5176\u4f59\u5b9a\u4e3a <strong><em>public <\/em><\/strong>\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u4f46\u4ecd\u53ef\u5728\u6700\u540e\u770b\u51fa\uff0c\u5728\u5916\u90e8\u53ef\u4ee5\u4f7f\u7528 <strong><em>_\u5b9a\u4e49\u8be5\u53d8\u91cf\u7684\u7c7b\u540d__\u53d8\u91cf\u540d<\/em><\/strong> \u8fd9\u4e2a\u65b9\u6cd5\u6765\u5728\u5916\u90e8\u8bbf\u95ee\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u53d8\u91cf\uff08\u7236\u7c7b\u3001\u5b50\u7c7b\u5185\u90e8\u5219\u76f4\u63a5\u4f7f\u7528<strong><em>\u53cc\u4e0b\u5212\u7ebf+\u53d8\u91cf\u540d<\/em><\/strong>\u5373\u53ef\uff09\uff0c\u800c\u5355\u4e0b\u5212\u7ebf\u53ef\u968f\u610f\u5728\u5916\u90e8\u3001\u5185\u90e8\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728python\u4e2d\uff0c\u5c0f\u5230\u57fa\u7840\u7c7b\u578b\uff0c\u5927\u5230\u5185\u7f6e\u65b9\u6cd5\u51fd\u6570\u3001\u7c7b\u7b49\u5168\u90fd\u662f\u5bf9\u8c61\u3002 \u5c31\u8fdetype\u7684\u63cf\u8ff0\u5b9e\u9645\u4e0a\u4e5f\u662f\u4e00\u4e2a\u7c7b\uff08bui [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":6,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"predecessor-version":[{"id":555,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/549\/revisions\/555"}],"wp:attachment":[{"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himpqblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}