

{"id":125740,"date":"2023-11-09T17:03:35","date_gmt":"2023-11-09T11:33:35","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=125740"},"modified":"2024-02-28T12:13:06","modified_gmt":"2024-02-28T06:43:06","slug":"python-program-on-__init__-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/","title":{"rendered":"Python Program on __init__() Function"},"content":{"rendered":"<p>Python&#8217;s object-oriented paradigm is built on the concept of classes and instances, offering a powerful way to structure code and encapsulate data. In this exploration, we delve into the code snippet that exemplifies the utilization of the &#8216;self&#8217; variable within a Python class. By creating instances of the class and employing the &#8216;self&#8217; variable, we unlock the ability to manage individualized data for each instance.<\/p>\n<p>This article navigates through the intricacies of the &#8216;self&#8217; variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Basics of object-oriented programming (OOP)<\/li>\n<li>Familiarity with Python syntax and class structure<\/li>\n<li>Understanding of instance variables and methods in Python classes<\/li>\n<li>Knowledge of string formatting in Python<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>The provided code introduces the &#8216;Test&#8217; class, featuring an &#8216;init&#8217; method that initializes three instance variables: &#8216;rno,&#8217; &#8216;name,&#8217; and &#8216;clgname.&#8217; The essence of the &#8216;self&#8217; variable becomes apparent here, as it binds these variables to the instance of the class. When instances &#8216;T1&#8217; and &#8216;T2&#8217; are created, the &#8216;init&#8217; method is invoked for each, setting their unique attributes. The &#8216;display&#8217; method, utilizing &#8216;self,&#8217; then prints these attributes for each instance, demonstrating the instance-specific nature of the data.<\/p>\n<p>Furthermore, the &#8216;str&#8217; method is defined within the class, showcasing another facet of the &#8216;self&#8217; variable. This method provides a string representation of the instance, formatting the output to display the roll number, name, and college name. When &#8216;print(T1)&#8217; and &#8216;print(T2)&#8217; are executed, the &#8216;str&#8217; method is implicitly called, offering a concise and informative representation of the instances&#8217; data. This underscores the versatility of the &#8216;self&#8217; variable in not only accessing but also customizing how instances present themselves.<\/p>\n<h4>Program Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Use of Self Variable in Python\r\nclass Test:\r\n    def __init__(self):\r\n        # Initializing instance variables using the constructor method\r\n        print(\"This is init method of class\")\r\n        self.rno = 101       # Initializing roll number\r\n        self.name = \"Vivek\"  # Initializing name\r\n        self.clgname = \"Data Flair Indore\"  # Initializing college name\r\n\r\n    def display(self):\r\n        # Method to display instance variables\r\n        print(self.rno)\r\n        print(self.name)\r\n        print(self.clgname)\r\n\r\n    def __str__(self):\r\n        # Using % formatting\r\n        return \"Roll No= %s Name= %s College Name=%s\" % (self.rno, self.name, self.clgname)\r\n\r\n# Creating instances of the Test class\r\nT1 = Test()\r\nT2 = Test()\r\n\r\n# Displaying the custom string representation of the objects\r\nprint(T1)\r\nprint(T2)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>This is init method of class<br \/>\nThis is init method of class<br \/>\nRoll No= 101 Name= Vivek College Name=Data Flair Indore<br \/>\nRoll No= 101 Name= Vivek College Name=Data Flair Indore<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>&#8220;This is init method of class&#8221; is printed twice because the init() method prints this string<br \/>\nIt gets printed twice because init() is called both when the Test instance T1 is created, and when the Child instance C1 is created<\/li>\n<li>&#8220;Roll No= 101 Name= Vivek College Name=Data Flair Indore&#8221; is printed twice, once when T1.displaydata() is called, and again when C1.displaydata() is called<\/li>\n<li>The roll no 101, name Vivek, and college name Data Flair Indore are printed because they were assigned to self in the init() method<\/li>\n<li>When displaydata() is called on T1, it prints out the values assigned to self for that instance<\/li>\n<li>Similarly when displaydata() is called on C1, it prints out the values assigned to self for that instance<\/li>\n<li>So the self variable allows each object instance to store state that is unique to that particular instance<br \/>\nAnd self allows methods like displaydata() to access the state of the instance they are called on, to display values specific to that object<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In conclusion, the &#8216;self&#8217; variable in Python classes is not just a syntactic necessity but a powerful tool that enables the creation of dynamic, individualized instances. Through the provided code, we witnessed how &#8216;self&#8217; ensures the uniqueness of instance attributes and how it enhances the representation of instances via the &#8216;str&#8217; method. This article aimed to demystify the role of &#8216;self&#8217; in creating more intuitive and versatile Python classes, showcasing its impact on both instance creation and user-friendly outputs. As Python remains a language valued for its simplicity and readability, understanding the nuances of &#8216;self&#8217; contributes to writing more robust and expressive code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s object-oriented paradigm is built on the concept of classes and instances, offering a powerful way to structure code and encapsulate data. In this exploration, we delve into the code snippet that exemplifies the&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28853,10333,28854,28626,28852],"class_list":["post-125740","post","type-post","status-publish","format-standard","hentry","category-python","tag-init-function-in-python","tag-python","tag-python-init-function","tag-python-practical","tag-python-program-on-init-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on __init__() Function - DataFlair<\/title>\n<meta name=\"description\" content=\"This article navigates through the intricacies of the &#039;self&#039; variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on __init__() Function - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This article navigates through the intricacies of the &#039;self&#039; variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-09T11:33:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:43:06+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on __init__() Function - DataFlair","description":"This article navigates through the intricacies of the 'self' variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.","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:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on __init__() Function - DataFlair","og_description":"This article navigates through the intricacies of the 'self' variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-09T11:33:35+00:00","article_modified_time":"2024-02-28T06:43:06+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on __init__() Function","datePublished":"2023-11-09T11:33:35+00:00","dateModified":"2024-02-28T06:43:06+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/"},"wordCount":582,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["init function in python","Python","python init function","python practical","Python Program on init Function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/","name":"Python Program on __init__() Function - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-09T11:33:35+00:00","dateModified":"2024-02-28T06:43:06+00:00","description":"This article navigates through the intricacies of the 'self' variable, showcasing its role in initializing, accessing, and representing instance-specific attributes.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-__init__-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on __init__() Function"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125740","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=125740"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125740\/revisions"}],"predecessor-version":[{"id":134156,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125740\/revisions\/134156"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=125740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=125740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=125740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}