{"id":130342,"date":"2023-05-10T18:00:40","date_gmt":"2023-05-10T12:30:40","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=130342"},"modified":"2024-04-12T14:33:59","modified_gmt":"2024-04-12T09:03:59","slug":"object-methods-and-the-init-method","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/","title":{"rendered":"Object methods and the init method"},"content":{"rendered":"\n<p><strong>Object methods and the init method<\/strong><\/p>\n\n\n\n<p>In Python, the <code>__init__()<\/code> method is a special method that gets called when an object is created from a class. It is used to initialize the attributes of the object.<\/p>\n\n\n\n<p>Here&#8217;s an example of a class with an <code>__init__()<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n    \n    def say_hello(self):\n        print(f\"Hello, my name is {self.name} and I'm {self.age} years old.\")\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>__init__()<\/code> method takes two arguments, <code>name<\/code> and <code>age<\/code>, and initializes the <code>name<\/code> and <code>age<\/code> attributes of the object.<\/p>\n\n\n\n<p>To create a new object from this class, we call the class like a function, passing any required arguments:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>p = Person(\"Alice\", 25)\n<\/code><\/pre>\n\n\n\n<p>This creates a new <code>Person<\/code> object with the <code>name<\/code> attribute set to &#8220;Alice&#8221; and the <code>age<\/code> attribute set to 25.<\/p>\n\n\n\n<p>We can then call the object&#8217;s methods, like <code>say_hello()<\/code>, which can access and operate on the object&#8217;s attributes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>p.say_hello()  # prints \"Hello, my name is Alice and I'm 25 years old.\"\n<\/code><\/pre>\n\n\n\n<p>In addition to the <code>__init__()<\/code> method, we can define other methods on a class that operate on the object&#8217;s attributes. These methods take the <code>self<\/code> parameter, which refers to the object that the method is being called on.<\/p>\n\n\n\n<p>For example, here&#8217;s a class with a <code>have_birthday()<\/code> method that increments the <code>age<\/code> attribute of the object:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>class Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n    \n    def say_hello(self):\n        print(f\"Hello, my name is {self.name} and I'm {self.age} years old.\")\n    \n    def have_birthday(self):\n        self.age += 1\n<\/code><\/pre>\n\n\n\n<p>We can call the <code>have_birthday()<\/code> method on a <code>Person<\/code> object to increment its <code>age<\/code> attribute:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>p = Person(\"Alice\", 25)\np.have_birthday()\np.say_hello()  # prints \"Hello, my name is Alice and I'm 26 years old.\"\n<\/code><\/pre>\n\n\n\n<p>Overall, the <code>__init__()<\/code> method and other methods defined on a class allow us to create and manipulate objects in Python. They are a key part of object-oriented programming in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object methods and the init method In Python, the __init__() method is a special method that gets called when an object is created from a class. It is used to initialize the attributes of the object. Here&#8217;s an example of a class with an __init__() method: pythonCopy codeclass Person: def __init__(self, name, age): self.name =&#8230;<\/p>\n","protected":false},"author":23,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[252],"tags":[],"class_list":["post-130342","page","type-page","status-publish","hentry","category-python-developer"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Object methods and the init method - Tutorial<\/title>\n<meta name=\"description\" content=\"Object methods and the init method\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Object methods and the init method - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Object methods and the init method\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T09:03:59+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/\",\"name\":\"Object methods and the init method - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2023-05-10T12:30:40+00:00\",\"dateModified\":\"2024-04-12T09:03:59+00:00\",\"description\":\"Object methods and the init method\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Object methods and the init method\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Object methods and the init method - Tutorial","description":"Object methods and the init method","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/","og_locale":"en_US","og_type":"article","og_title":"Object methods and the init method - Tutorial","og_description":"Object methods and the init method","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T09:03:59+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/","name":"Object methods and the init method - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2023-05-10T12:30:40+00:00","dateModified":"2024-04-12T09:03:59+00:00","description":"Object methods and the init method","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/object-methods-and-the-init-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Object methods and the init method"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/130342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=130342"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/130342\/revisions"}],"predecessor-version":[{"id":130345,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/130342\/revisions\/130345"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=130342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=130342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=130342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}