{"id":2398,"date":"2023-01-31T16:50:44","date_gmt":"2023-01-31T16:50:44","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2398"},"modified":"2023-01-31T16:50:45","modified_gmt":"2023-01-31T16:50:45","slug":"python-class","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/01\/31\/python-class\/","title":{"rendered":"Python: Class"},"content":{"rendered":"\n<p>Learning to write Python code? A Python class is a blueprint for creating objects. <\/p>\n\n\n\n<p>It defines a set of attributes and methods that can be used to manipulate those attributes. Here&#8217;s an example of a simple Python class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Dog:\n    def __init__(self, name, breed):\n        self.name = name\n        self.breed = breed\n\n    def bark(self):\n        print(\"Woof!\")<\/code><\/pre>\n\n\n\n<p>The <code>class<\/code> keyword is used to define a new class. The name of the class is <code>Dog<\/code>.<\/p>\n\n\n\n<p>The <code>__init__<\/code> method is a special method in Python that is used to initialize an object when it is created. The <code>self<\/code> parameter refers to the instance of the object being created.<\/p>\n\n\n\n<p>The <code>name<\/code> and <code>breed<\/code> attributes are created within the <code>__init__<\/code> method and can be accessed using the <code>self<\/code> reference.<\/p>\n\n\n\n<p>The <code>bark<\/code> method is an example of a custom method in the class. It can be called on an instance of the <code>Dog<\/code> class to print &#8220;Woof!&#8221;.<\/p>\n\n\n\n<p>To create an instance of the class, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dog = Dog(\"Fido\", \"Labrador\")\ndog.bark()\n# Output: Woof!<\/code><\/pre>\n\n\n\n<p>This creates a new instance of the <code>Dog<\/code> class, with the <code>name<\/code> attribute set to &#8220;Fido&#8221; and the <code>breed<\/code> attribute set to &#8220;Labrador&#8221;. <\/p>\n\n\n\n<p>The <code>bark<\/code> method is then called on the <code>dog<\/code> instance to produce the output &#8220;Woof!&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning to write Python code? A Python class is a blueprint for creating objects. It defines a set of attributes and methods that can be used to manipulate those attributes. Here&#8217;s an example of a simple Python class: The class keyword is used to define a new class. The name of the class is Dog. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[270],"class_list":["post-2398","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2398"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2398\/revisions"}],"predecessor-version":[{"id":2400,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2398\/revisions\/2400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}