{"id":177010,"date":"2024-05-08T19:22:03","date_gmt":"2024-05-08T11:22:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/177010.html"},"modified":"2024-05-08T19:22:08","modified_gmt":"2024-05-08T11:22:08","slug":"python-%e7%9a%84-type-%e5%92%8c-object-%e4%b9%8b%e9%97%b4%e6%98%af%e6%80%8e%e4%b9%88%e4%b8%80%e7%a7%8d%e5%85%b3%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/177010.html","title":{"rendered":"Python \u7684 type \u548c object \u4e4b\u95f4\u662f\u600e\u4e48\u4e00\u79cd\u5173\u7cfb"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27060850\/44b48980-193a-4199-a3eb-81d1e53f0536.webp\" alt=\"Python \u7684 type \u548c object \u4e4b\u95f4\u662f\u600e\u4e48\u4e00\u79cd\u5173\u7cfb\" \/><\/p>\n<p><p>Python\u4e2d\uff0c<strong>type<\/strong> \u548c <strong>object<\/strong> \u662f\u57fa\u7840\u6784\u9020\u5757\uff0c\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u7740\u5bc6\u5207\u7684\u5173\u7cfb\u3002<strong>type<\/strong> \u662fPython\u7684\u5185\u7f6e\u5143\u7c7b\uff0c\u8d1f\u8d23\u751f\u6210\u65b0\u7684\u7c7b\uff0c\u800c<strong>object<\/strong> \u662f\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\u3002\u6bcf\u4e2a\u7c7b\u90fd\u662f\u7531 <strong>type<\/strong> \u521b\u5efa\u7684\uff0c\u6bcf\u4e2a\u5bf9\u8c61\uff08\u5305\u62ec\u7c7b\uff09\u90fd\u662f <strong>object<\/strong> \u7684\u5b9e\u4f8b\u3002\u8fd9\u521b\u5efa\u4e86\u4e00\u4e2a\u6709\u8da3\u7684\u6a21\u578b\uff0c\u5176\u4e2d <strong>type<\/strong> \u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u7c7b\uff0c\u5b83\u662f\u5b83\u81ea\u5df1\u7684\u5b9e\u4f8b\uff0c\u800c <strong>object<\/strong> \u662f\u6240\u6709Python\u7c7b\u7684\u9876\u5c42\u6839\u7c7b\u3002\u8fd9\u6784\u6210\u4e86Python\u5bf9\u8c61\u6a21\u578b\u7684\u6838\u5fc3\uff1a<strong>\u7c7b\u662ftype\u7684\u5b9e\u4f8b\uff0c\u5bf9\u8c61\u662f\u7c7b\u7684\u5b9e\u4f8b\uff0c\u800ctype\u548cobject\u90fd\u7ee7\u627f\u81eaobject<\/strong>\uff0c\u5f62\u6210\u4e86Python\u7684\u4e00\u4e2a\u5faa\u73af\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3type\u548cobject\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><h4>type\u7684\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p><strong>type<\/strong> \u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u5143\u7c7b\uff0c\u5728Python\u4e2d\u7528\u4e8e\u521b\u5efa\u6240\u6709\u7684\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7type(name, bases, dict)\u8fd9\u6837\u7684\u65b9\u5f0f\u52a8\u6001\u521b\u5efa\u7c7b\u3002\u5176\u4e2d\uff0cname\u662f\u7c7b\u540d\uff0cbases\u662f\u57fa\u7c7b\u7684\u5143\u7ec4\uff0cdict\u662f\u5305\u542b\u7c7b\u4f53\u5b9a\u4e49\u7684\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h4>object\u7684\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p><strong>object<\/strong> \u662f\u6240\u6709Python\u7c7b\u7684\u57fa\u7c7b\uff0c\u4efb\u4f55\u65b0\u5b9a\u4e49\u7684\u7c7b\u548c\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u6ca1\u6709\u660e\u786e\u6307\u5b9a\u57fa\u7c7b\uff0c\u90a3\u4e48\u5b83\u4eec\u9ed8\u8ba4\u7ee7\u627f\u81eaobject\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5143\u7c7b\u548c\u666e\u901a\u7c7b\u7684\u5173\u7cfb<\/h3>\n<\/p>\n<p><h4>\u5143\u7c7b\u7684\u4f5c\u7528<\/h4>\n<\/p>\n<p><p>\u5143\u7c7b\u5728Python\u4e2d\u7528\u4e8e\u5b9a\u4e49\u521b\u5efa\u7c7b\u7684\u884c\u4e3a\u3002\u5143\u7c7b\u53ef\u4ee5\u63a7\u5236\u7c7b\u7684\u5b9a\u4e49\u8fc7\u7a0b\uff0c\u5305\u62ec\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u6dfb\u52a0\u3001\u7ee7\u627f\u5173\u7cfb\u7684\u7ba1\u7406\u7b49\u3002<strong>type<\/strong> \u4f5c\u4e3aPython\u7684\u9ed8\u8ba4\u5143\u7c7b\uff0c\u5f53\u6211\u4eec\u6ca1\u6709\u6307\u5b9a\u5143\u7c7b\u65f6\uff0cPython\u4f1a\u4f7f\u7528type\u6765\u521b\u5efa\u6240\u6709\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>\u7c7b\u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b<\/h4>\n<\/p>\n<p><p>\u5f53\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u6bd4\u5982class MyClass(object): pass\uff0cPython\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7\u8c03\u7528type\u6765\u521b\u5efa\u8fd9\u4e2a\u7c7b\u3002MyClass\u73b0\u5728\u662ftype\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u540c\u65f6\uff0c\u4efb\u4f55\u57fa\u4e8eMyClass\u5b9e\u4f8b\u5316\u7684\u5bf9\u8c61\uff0c\u662fMyClass\u7684\u5b9e\u4f8b\uff0c\u4e5f\u95f4\u63a5\u662fobject\u7684\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001type\u4e0eobject\u7684\u5faa\u73af\u5173\u7cfb<\/h3>\n<\/p>\n<p><h4>type\u662f\u81ea\u8eab\u7684\u5b9e\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e00\u4e2a\u6709\u8da3\u7684\u73b0\u8c61\u662f\uff0c<strong>type<\/strong> \u662f\u5b83\u81ea\u5df1\u7684\u5b9e\u4f8b\uff0c\u5373type\u662f\u7531\u5b83\u81ea\u5df1\u521b\u5efa\u7684\u3002\u8fd9\u5728\u7f16\u7a0b\u8bed\u8a00\u4e2d\u662f\u4e00\u4e2a\u4e0d\u5e38\u89c1\u7684\u7279\u6027\uff0c\u4f46\u5728Python\u4e2d\u5374\u662f\u5bf9\u8c61\u6a21\u578b\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u6362\u53e5\u8bdd\u8bf4\uff0ctype\u662f\u4e00\u4e2a\u7c7b\uff0c\u800c\u8fd9\u4e2a\u7c7b\u7684\u7c7b\u578b(type)\u4e5f\u662f\u5b83\u81ea\u5df1\u3002<\/p>\n<\/p>\n<p><h4>object\u548ctype\u7684\u5173\u8054<\/h4>\n<\/p>\n<p><p>object\u867d\u7136\u662f\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\uff0c\u4f46object\u4e5f\u662f\u7531type\u521b\u5efa\u7684\uff0c\u5373object\u662ftype\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5b83\u4eec\u7684\u5173\u7cfb\u6784\u6210\u4e86Python\u5bf9\u8c61\u6a21\u578b\u7684\u57fa\u77f3\u3002\u8fd9\u79cd\u8bbe\u8ba1\u8ba9Python\u62e5\u6709\u4e86\u975e\u5e38\u7075\u6d3b\u548c\u52a8\u6001\u7684\u7c7b\u578b\u4f53\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u5177\u4f53\u4ee3\u7801\u7406\u89e3\u5173\u7cfb<\/h3>\n<\/p>\n<p><h4>\u67e5\u770btype\u548cobject\u7684\u5173\u7cfb<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">print(isinstance(object, type))  # object\u662f\u4e0d\u662ftype\u7684\u5b9e\u4f8b<\/p>\n<p>print(isinstance(type, object))  # type\u662f\u4e0d\u662fobject\u7684\u5b9e\u4f8b<\/p>\n<p>print(type(&#039;MyClass&#039;, (object,), {}))  # \u901a\u8fc7type\u521b\u5efa\u7c7b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1type\u4e0eobject\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u7406\u89e3\u5982\u4f55\u901a\u8fc7type\u521b\u5efa\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5143\u7c7b<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u81ea\u5b9a\u4e49\u5143\u7c7b\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7406\u89e3type\u548cobject\u7684\u5173\u7cfb\u4ee5\u53ca\u5143\u7c7b\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyMeta(type):<\/p>\n<p>    pass<\/p>\n<p>class MyClass(metaclass=MyMeta):<\/p>\n<p>    pass<\/p>\n<p>print(isinstance(MyClass, MyMeta))  # MyClass\u662f\u4e0d\u662fMyMeta\u7684\u5b9e\u4f8b<\/p>\n<p>print(isinstance(MyMeta, type))     # MyMeta\u662f\u4e0d\u662ftype\u7684\u5b9e\u4f8b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ee7\u627ftype\u521b\u5efa\u81ea\u5b9a\u4e49\u5143\u7c7b\uff0c\u5e76\u4f7f\u7528\u8be5\u5143\u7c7b\u6765\u521b\u5efa\u7c7b\uff0c\u53ef\u4ee5\u6df1\u5165\u7406\u89e3Python\u4e2d\u5143\u7c7b\u7684\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001type\u548cobject\u5728\u7ee7\u627f\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>\u591a\u91cd\u7ee7\u627f\u7684\u51b2\u7a81\u4e0e\u89e3\u51b3<\/h4>\n<\/p>\n<p><p>Python\u7684\u591a\u91cd\u7ee7\u627f\u4e2d\uff0ctype\u548cobject\u7684\u5173\u7cfb\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u65b9\u6cd5\u89e3\u6790\u987a\u5e8f\uff08MRO\uff09\u3002Python\u7684C3\u7ebf\u6027\u5316\u7b97\u6cd5\u7528\u6765\u89e3\u51b3\u590d\u6742\u7684\u7ee7\u627f\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h4>\u7c7b\u578b\u68c0\u67e5\u4e0e\u62bd\u8c61\u57fa\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u63a5\u53e3\u6216\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u65f6\uff0cobject\u4e3a\u6240\u6709\u7c7b\u63d0\u4f9b\u4e86\u4e00\u81f4\u7684\u6846\u67b6\u3002\u901a\u8fc7\u7ee7\u627fobject\uff0c\u53ef\u4ee5\u786e\u4fdd\u9075\u5faaPython\u7684\u6570\u636e\u6a21\u578b\u548c\u884c\u4e3a\u4e00\u81f4\u6027\uff0c\u540c\u65f6\uff0ctype\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u62bd\u8c61\u57fa\u7c7b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8be6\u7ec6\u89e3\u8bfbtype\u548cobject\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0Python\u7684\u5bf9\u8c61\u6a21\u578b\u662f\u5982\u4f55\u6784\u5efa\u7684\uff0c\u4ee5\u53ca\u8fd9\u4e00\u8bbe\u8ba1\u5e26\u6765\u7684\u7075\u6d3b\u6027\u548c\u52a8\u6001\u6027\u3002type\u548cobject\u4e0d\u4ec5\u4ec5\u662fPython\u4e2d\u4e24\u4e2a\u7279\u522b\u7684\u5b9e\u4f53\uff0c\u5b83\u4eec\u4e4b\u95f4\u590d\u6742\u7684\u5173\u7cfb\u6784\u6210\u4e86Python\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Python \u4e2d\u7684 type \u548c object \u6709\u4ec0\u4e48\u5173\u8054\uff1f<\/strong><\/p>\n<p>Python \u4e2d\u7684 type \u548c object \u662f\u7d27\u5bc6\u76f8\u5173\u7684\u3002\u5728 Python \u4e2d\uff0c\u4e00\u5207\u7686\u5bf9\u8c61\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7c7b\u578b\u3002type \u662f Python \u4e2d\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u800c object \u5219\u662f\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\uff0c\u4e5f\u88ab\u79f0\u4e3a\u6240\u6709\u5bf9\u8c61\u7684\u7956\u5148\u3002<\/p>\n<p><strong>2. type \u548c object \u5bf9\u4e8e Python \u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u6709\u4f55\u5f71\u54cd\uff1f<\/strong><\/p>\n<p>\u5728 Python \u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0ctype \u548c object \u662f\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\u3002type \u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u83b7\u53d6\u5bf9\u8c61\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u800c object \u5219\u4f5c\u4e3a\u6240\u6709\u7c7b\u7684\u57fa\u7c7b\uff0c\u4e3a\u6240\u6709\u5bf9\u8c61\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p><strong>3. type \u548c object \u5728 Python \u4e2d\u80fd\u591f\u5b9e\u73b0\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><\/p>\n<p>type \u5728 Python \u4e2d\u62e5\u6709\u591a\u91cd\u529f\u80fd\u3002\u9996\u5148\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u901a\u8fc7 type(obj) \u53ef\u4ee5\u83b7\u53d6 obj \u7684\u7c7b\u578b\u3002\u5176\u6b21\uff0ctype \u8fd8\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u5730\u521b\u5efa\u7c7b\uff0c\u901a\u8fc7 type(name, bases, dict) \u7684\u65b9\u5f0f\u521b\u5efa\u7c7b\u5bf9\u8c61\u3002\u6700\u540e\uff0ctype \u8fd8\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u7c7b\u6216\u5bf9\u8c61\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<p>\u800c object \u5728 Python \u4e2d\u4e5f\u6709\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002\u6240\u6709\u7684\u7c7b\u90fd\u662f object \u7684\u5b50\u7c7b\uff0c\u7ee7\u627f\u4e86 object \u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\u4f7f\u7528 object \u63d0\u4f9b\u7684\u529f\u80fd\uff0c\u6bd4\u5982__str__\u3001<strong>eq<\/strong>\u3001<strong>hash<\/strong> \u7b49\u65b9\u6cd5\uff0c\u5b9e\u73b0\u66f4\u9ad8\u7ea7\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\uff0ctype \u548c object \u662f\u57fa\u7840\u6784\u9020\u5757\uff0c\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u7740\u5bc6\u5207\u7684\u5173\u7cfb\u3002type \u662fPython\u7684 [&hellip;]","protected":false},"author":3,"featured_media":177017,"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\/177010"}],"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=177010"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/177010\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/177017"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=177010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=177010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=177010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}