{"id":1651,"date":"2023-07-13T15:59:21","date_gmt":"2023-07-13T07:59:21","guid":{"rendered":"http:\/\/zhang.mba\/?p=1651"},"modified":"2023-07-13T16:20:15","modified_gmt":"2023-07-13T08:20:15","slug":"python-zhong-deisinstance","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/07\/13\/15\/59\/21\/1651\/python-zhong-deisinstance\/python\/zhangzhiqi\/","title":{"rendered":"Python\u4e2d\u7684isinstance()"},"content":{"rendered":"<p>isinstance() \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u6307\u5b9a\u7684\u7c7b\u578b\uff08\u6216\u5176\u5b50\u7c7b\uff09\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">isinstance(object, classinfo)\n<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cobject \u662f\u8981\u68c0\u67e5\u7684\u5bf9\u8c61\uff0cclassinfo \u662f\u8981\u68c0\u67e5\u7684\u7c7b\u578b\uff08\u53ef\u4ee5\u662f\u5355\u4e2a\u7c7b\u578b\u6216\u7c7b\u578b\u5143\u7ec4\uff09\u3002<\/p>\n<p>isinstance() \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u6216\u903b\u8f91\u5224\u65ad\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u6761\u4ef6\u5224\u65ad\u3001\u7c7b\u578b\u9a8c\u8bc1\u548c\u591a\u6001\u5904\u7406\u7b49\u573a\u666f\u3002\u4e0b\u9762\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u7528\u9014\uff1a<\/p>\n<p>\u6761\u4ef6\u5224\u65ad\uff1a\u901a\u8fc7 isinstance() \u53ef\u4ee5\u6839\u636e\u5bf9\u8c61\u7684\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5f53\u5bf9\u8c61\u662f\u5217\u8868\u65f6\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\uff0c\u5f53\u5bf9\u8c61\u662f\u5b57\u5178\u65f6\u6267\u884c\u53e6\u4e00\u4e2a\u64cd\u4f5c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">if isinstance(my_object, list):\n    # \u5904\u7406\u5217\u8868\u5bf9\u8c61\u7684\u903b\u8f91\nelif isinstance(my_object, dict):\n    # \u5904\u7406\u5b57\u5178\u5bf9\u8c61\u7684\u903b\u8f91\n<\/code><\/pre>\n<p>\u7c7b\u578b\u9a8c\u8bc1\uff1a\u53ef\u4ee5\u4f7f\u7528 isinstance() \u6765\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u662f\u5426\u7b26\u5408\u9884\u671f\uff0c\u4ee5\u786e\u4fdd\u51fd\u6570\u5728\u8fd0\u884c\u65f6\u63a5\u6536\u5230\u6b63\u786e\u7684\u53c2\u6570\u7c7b\u578b\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">def process_data(data):\n    if not isinstance(data, list):\n        raise ValueError(&quot;Data must be a list.&quot;)\n    # \u5904\u7406\u6570\u636e\u7684\u903b\u8f91\n\n# \u8c03\u7528\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u5408\u6cd5\u7684\u53c2\u6570\nmy_data = [1, 2, 3]\nprocess_data(my_data)\n\n# \u8c03\u7528\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u975e\u6cd5\u7684\u53c2\u6570\nmy_data = &quot;invalid&quot;\nprocess_data(my_data)  # \u629b\u51fa\u5f02\u5e38\n<\/code><\/pre>\n<p>\u591a\u6001\u5904\u7406\uff1a\u5f53\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u5229\u7528 isinstance() \u6765\u5b9e\u73b0\u591a\u6001\u3002\u901a\u8fc7\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u9009\u62e9\u76f8\u5e94\u7684\u64cd\u4f5c\u6216\u65b9\u6cd5\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">class Animal:\n    def sound(self):\n        pass\n\nclass Dog(Animal):\n    def sound(self):\n        print(&quot;Woof!&quot;)\n\nclass Cat(Animal):\n    def sound(self):\n        print(&quot;Meow!&quot;)\n\n# \u591a\u6001\u5904\u7406\nanimals = [Dog(), Cat(), Dog(), Cat()]\n\nfor animal in animals:\n    animal.sound()\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6839\u636e\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8c03\u7528\u4e0d\u540c\u5b50\u7c7b\u7684 sound() \u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u591a\u6001\u7684\u6548\u679c\u3002<\/p>\n<p>\u603b\u4e4b\uff0cisinstance() \u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u68c0\u67e5\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u53ef\u4ee5\u7528\u4e8e\u6761\u4ef6\u5224\u65ad\u3001\u7c7b\u578b\u9a8c\u8bc1\u548c\u591a\u6001\u5904\u7406\u7b49\u573a\u666f\uff0c\u4ee5\u63d0\u4f9b\u66f4\u7075\u6d3b\u548c\u5065\u58ee\u7684\u4ee3\u7801\u903b\u8f91\u3002<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>isinstance() \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u68c0\u67e5\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u5c5e\u4e8e\u6307\u5b9a\u7684\u7c7b\u578b\uff08\u6216\u5176\u5b50\u7c7b\uff09\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[12],"tags":[61],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1651"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1651"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1651\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}