{"id":699,"date":"2024-01-06T21:56:43","date_gmt":"2024-01-06T13:56:43","guid":{"rendered":"http:\/\/madapapa.com\/wordpress\/?p=699"},"modified":"2024-01-06T23:22:20","modified_gmt":"2024-01-06T15:22:20","slug":"python-declassinstance","status":"publish","type":"post","link":"http:\/\/madapapa.com\/wordpress\/?p=699","title":{"rendered":"python\u7684class\u3001instance"},"content":{"rendered":"<h3><a id=\"instance\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Instance:<\/h3>\n<p>In Python, an instance is a specific object created from a class. A class is a blueprint that defines the members (attributes and methods) it supports, and an instance is a concrete object created based on this blueprint. Each instance has the attributes and methods defined in the class.<br \/>\nExample: If you have a Dog class, when you create a Dog object, that object is an instance of the Dog class.<\/p>\n<pre><code class=\"language-plain_text\">class Dog:\n    def __init__(self, name):\n        self.name = name\n\nmy_dog = Dog(&quot;Buddy&quot;)  # my_dog is an instance of the Dog class\n<\/code><\/pre>\n<h3><a id=\"object\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Object:<\/h3>\n<p>In Python, an object is the fundamental unit in the program, and almost everything is an object, including numbers, strings, data structures, functions, and even classes themselves. Each object has a type (indicating what it is) and identity (a unique identifier).<br \/>\nExample: In the Dog class above, my_dog is not just an instance, but also an object.<\/p>\n<h3><a id=\"item\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Item:<\/h3>\n<p>In Python, an item typically refers to a single element within a container type like a list, tuple, or dictionary.<br \/>\nExample: In a list, each element at a position is an item.<\/p>\n<pre><code class=\"language-plain_text\">numbers = [1, 2, 3]\nfirst_item = numbers[0]  # first_item is an item in the list, with a value of 1\n<\/code><\/pre>\n<h3><a id=\"attribute\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Attribute:<\/h3>\n<p>In Python, an attribute is a variable that belongs to an object. In a class definition, attributes are variables defined outside of methods and are used to store the state of an instance.<br \/>\nExample: In the Dog class, name is an attribute.<\/p>\n<pre><code class=\"language-plain_text\">class Dog:\n    def __init__(self, name):\n        self.name = name  # name is an attribute\n\nmy_dog = Dog(&quot;Buddy&quot;)\nprint(my_dog.name)  # Accessing the name attribute of my_dog\n<\/code><\/pre>\n<p>In this example, Dog is a class, my_dog is an instance of the Dog class (and also an object), name is an attribute of the my_dog object, and [1, 2, 3] is a list where 1, 2, and 3 are items in the list.<\/p>\n<h2><a id=\"%E4%B8%AD%E6%96%87%E8%A7%A3%E9%87%8A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4e2d\u6587\u89e3\u91ca<\/h2>\n<p>\u8ba9\u6211\u4eec\u7ed3\u5408Python\u8bed\u8a00\u6765\u91cd\u65b0\u89e3\u91ca\u8fd9\u4e9b\u6982\u5ff5\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4f8b\u5b50\uff1a<\/p>\n<h3><a id=\"%E5%AE%9E%E4%BE%8B%EF%BC%88instance%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5b9e\u4f8b\uff08Instance\uff09\uff1a<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u5b9e\u4f8b\u662f\u6839\u636e\u7c7b\u521b\u5efa\u7684\u5177\u4f53\u5bf9\u8c61\u3002\u7c7b\u662f\u4e00\u79cd\u5b9a\u4e49\u5176\u6210\u5458\uff08\u5c5e\u6027\u548c\u65b9\u6cd5\uff09\u7684\u6a21\u677f\uff0c\u800c\u5b9e\u4f8b\u662f\u6839\u636e\u8fd9\u4e2a\u6a21\u677f\u521b\u5efa\u7684\u5177\u4f53\u5bf9\u8c61\u3002\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u62e5\u6709\u7c7b\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<br \/>\n\u4f8b\u5b50\uff1a\u5982\u679c\u6709\u4e00\u4e2a\u7c7bDog\uff0c\u5f53\u4f60\u521b\u5efa\u4e00\u4e2aDog\u7684\u5bf9\u8c61\u65f6\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5c31\u662fDog\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<pre><code class=\"language-plain_text\">class Dog:\n    def __init__(self, name):\n        self.name = name\n\nmy_dog = Dog(&quot;Buddy&quot;)  # my_dog \u662f Dog \u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\n<\/code><\/pre>\n<h3><a id=\"%E5%AF%B9%E8%B1%A1%EF%BC%88object%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5bf9\u8c61\uff08Object\uff09\uff1a<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u5bf9\u8c61\u662f\u7a0b\u5e8f\u4e2d\u7684\u57fa\u672c\u5355\u5143\uff0c\u51e0\u4e4e\u6240\u6709\u4e1c\u897f\u90fd\u662f\u5bf9\u8c61\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u6570\u636e\u7ed3\u6784\u3001\u51fd\u6570\uff0c\u4ee5\u53ca\u7c7b\u672c\u8eab\u3002\u6bcf\u4e2a\u5bf9\u8c61\u90fd\u6709\u7c7b\u578b\uff08\u8868\u793a\u5b83\u662f\u4ec0\u4e48\uff09\u548c\u8eab\u4efd\uff08\u6807\u8bc6\u5b83\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\uff09\u3002<br \/>\n\u4f8b\u5b50\uff1a\u5728\u4e0a\u9762\u7684Dog\u7c7b\u4e2d\uff0cmy_dog\u4e0d\u4ec5\u662f\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n\u9879\u76ee\uff08Item\uff09\uff1a<br \/>\n\u5728Python\u4e2d\uff0c\u9879\u76ee\u901a\u5e38\u6307\u7684\u662f\u5bb9\u5668\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\uff09\u4e2d\u7684\u5355\u4e2a\u5143\u7d20\u3002<br \/>\n\u4f8b\u5b50\uff1a\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u9879\u76ee\u3002<\/p>\n<pre><code class=\"language-plain_text\">numbers = [1, 2, 3]\nfirst_item = numbers[0]  # first_item \u662f\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u9879\u76ee\uff0c\u503c\u4e3a 1\n<\/code><\/pre>\n<h3><a id=\"%E5%B1%9E%E6%80%A7%EF%BC%88attribute%EF%BC%89%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5c5e\u6027\uff08Attribute\uff09\uff1a<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u5c5e\u6027\u662f\u9644\u5c5e\u4e8e\u5bf9\u8c61\u7684\u53d8\u91cf\u3002\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\uff0c\u5c5e\u6027\u662f\u5b9a\u4e49\u5728\u65b9\u6cd5\u5916\u7684\u53d8\u91cf\uff0c\u5b83\u4eec\u7528\u4e8e\u5b58\u50a8\u5b9e\u4f8b\u7684\u72b6\u6001\u3002<br \/>\n\u4f8b\u5b50\uff1a\u5728Dog\u7c7b\u4e2d\uff0cname\u662f\u4e00\u4e2a\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"language-plain_text\">class Dog:\n    def __init__(self, name):\n        self.name = name  # name \u662f\u4e00\u4e2a\u5c5e\u6027\n\nmy_dog = Dog(&quot;Buddy&quot;)\nprint(my_dog.name)  # \u8bbf\u95ee my_dog \u7684 name \u5c5e\u6027\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cDog\u662f\u4e00\u4e2a\u7c7b\uff0cmy_dog\u662fDog\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff08\u540c\u65f6\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\uff09\uff0cname\u662fmy_dog\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u800c[1, 2, 3]\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u76841\u30012\u30013\u662f\u5217\u8868\u7684\u9879\u76ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Instance: In Python, an instance is a specific object created from a class. A class is a blueprint that defines the members (attributes and methods) it supports, and an instance is a concrete object created based on this blueprint. Each instance has the attributes and methods defined in the class. Example: If you have a &hellip; <a href=\"http:\/\/madapapa.com\/wordpress\/?p=699\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">python\u7684class\u3001instance<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","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,"footnotes":""},"categories":[48],"tags":[],"class_list":["post-699","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence"],"_links":{"self":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=699"}],"version-history":[{"count":2,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions"}],"predecessor-version":[{"id":701,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions\/701"}],"wp:attachment":[{"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/madapapa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}