{"id":856,"date":"2021-07-18T14:00:00","date_gmt":"2021-07-18T14:00:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=856"},"modified":"2021-09-09T11:27:15","modified_gmt":"2021-09-09T11:27:15","slug":"what-is-init-function-in-python","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/","title":{"rendered":"What is init function in Python ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"python-init\" class=\"color-purple\" style=\"text-align: justify;\">Python init():<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>In Python, __init__() method within a class is its constructor.Any method or variable that starts with double underscore is by default private.<\/li>\n<li>Python defines several standard private methods that have a special meaning, and __init__() is one of them. Some others are __str__(), __repr__(), __add__() and others.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h2 id=\"what-does-def-init-do-in-python\" class=\"color-purple\" style=\"text-align: justify;\">What does def init do in Python ?<\/h2>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>The __init__ function is known as a constructor, or initializer, and is automatically called when you create a new instance of a class. Within that function, the recently created object is assigned to the parameter \u201cself\u201d. The notation self.legs is an attribute called legs of the object in the variable self.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"subheading\" style=\"text-align: justify;\">\n<h2 id=\"sample-code\">Sample Code:<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-markdown code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markdown code-embed-code\">class my_class(object):<br\/>  def __init(self, x, y):<br\/>    self.x = x<br\/>    self.y = y<br\/>def f2(self):<br\/>    return self.x + self.y<br\/> a = my_class(10, 20)<br\/>print a.f2()<\/code><\/pre> <\/div>\n<\/div>\n<div class=\"subheading\" style=\"text-align: justify;\">\n<h2 id=\"output\">Output :<\/h2>\n<\/div>\n<div class=\"Output\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"python hljs\" data-lang=\"python\"><span class=\"nt\">30 \r\n<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<p style=\"text-align: justify;\">In the constructor __init__() and method f2(), a initial parameter self is a reference to a object that will later invoke methods at run time. Actually leaving out self parameter in the definition makes it not possible to call it on behalf of the object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Answer : In Python, __init__() method within a class is its constructor&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5608],"tags":[5706,5705,5707,195,491,360,203,199,214,198,209,5709,205,222,484,196,212,213,286,710,207,366,204,217,282,208,5656,5715,5647,5714,5713,5704,5712,5711,5620,5710,5708,5617,5619,5618,483,206,200,197,280,364,5716,5631],"class_list":["post-856","post","type-post","status-publish","format-standard","hentry","category-python","tag-__init__-python-3python-init-main","tag-__init__-py-python","tag-__init__-py-python-3","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-def-__init__self","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-lt-infotech-interview-questions-and-answers","tag-mavenir-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-prokarma-softech-pvt-ltd-interview-questions-and-answers","tag-python-class-definition","tag-python-class-definition-example","tag-python-class-init","tag-python-class-init-function","tag-python-class-init-method","tag-python-class-init-super","tag-python-class-initialization","tag-python-class-without-__init__","tag-python-data-science-interview-questions","tag-python-do-classes-need-init","tag-python-init-not-called","tag-python-interview-questions-and-answers-for-testers","tag-python-programming-questions","tag-python-telephonic-interview-questions","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-what-is-python-class-init","tag-xoriant-soluti-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What is init function in Python ? - Basic Python Interview Questions<\/title>\n<meta name=\"description\" content=\"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.\" \/>\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.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is init function in Python ? - Basic Python Interview Questions\" \/>\n<meta property=\"og:description\" content=\"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-18T14:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-09T11:27:15+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/\"},\"author\":{\"name\":\"Editor\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/4d5a581fb5470d1560324bddc5e8b757\"},\"headline\":\"What is init function in Python ?\",\"datePublished\":\"2021-07-18T14:00:00+00:00\",\"dateModified\":\"2021-09-09T11:27:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/\"},\"wordCount\":215,\"commentCount\":0,\"keywords\":[\"__init__ python 3python init main\",\"__init__.py python\",\"__init__.py python 3\",\"Accenture interview questions and answers\",\"Applied Materials interview questions and answers\",\"Atos interview questions and answers\",\"Capgemini interview questions and answers\",\"CASTING NETWORKS INDIA PVT LIMITED interview questions and answers\",\"CGI Group Inc interview questions and answers\",\"Chetu interview questions and answers\",\"Collabera Technologies interview questions and answers\",\"def __init__(self)\",\"Dell International Services India Pvt Ltd interview questions and answers\",\"Flipkart interview questions and answers\",\"Genpact interview questions and answers\",\"IBM interview questions and answers\",\"Indecomm Global Services interview questions and answers\",\"Infosys Technologies interview questions and answers\",\"L&amp;T Infotech interview questions and answers\",\"Mavenir interview questions and answers\",\"Mphasis interview questions and answers\",\"NetApp interview questions and answers\",\"Oracle Corporation interview questions and answers\",\"PeopleStrong interview questions and answers\",\"Persistent Systems interview questions and answers\",\"Prokarma Softech Pvt Ltd interview questions and answers\",\"python class definition\",\"python class definition example\",\"python class init\",\"python class init function\",\"python class init method\",\"python class init super\",\"python class initialization\",\"python class without __init__\",\"python data science interview questions\",\"python do classes need init\",\"python init not called\",\"python interview questions and answers for testers\",\"python programming questions\",\"python telephonic interview questions\",\"Reliance Industries Ltd interview questions and answers\",\"SAP Labs India Pvt Ltd interview questions and answers\",\"Tech Mahindra interview questions and answers\",\"UnitedHealth Group interview questions and answers\",\"Virtusa Consulting Services Pvt Ltd interview questions and answers\",\"Wells Fargo interview questions and answers\",\"what is python class init ?\",\"Xoriant Soluti interview questions and answers\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/\",\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/\",\"name\":\"What is init function in Python ? - Basic Python Interview Questions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#website\"},\"datePublished\":\"2021-07-18T14:00:00+00:00\",\"dateModified\":\"2021-09-09T11:27:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/python\\\/what-is-init-function-in-python\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#website\",\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/#\\\/schema\\\/person\\\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\\\/\\\/www.wikitechy.com\\\/interview-questions\\\/author\\\/editor\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is init function in Python ? - Basic Python Interview Questions","description":"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.","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.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What is init function in Python ? - Basic Python Interview Questions","og_description":"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/","og_site_name":"Wikitechy","article_published_time":"2021-07-18T14:00:00+00:00","article_modified_time":"2021-09-09T11:27:15+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/#article","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/"},"author":{"name":"Editor","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"headline":"What is init function in Python ?","datePublished":"2021-07-18T14:00:00+00:00","dateModified":"2021-09-09T11:27:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/"},"wordCount":215,"commentCount":0,"keywords":["__init__ python 3python init main","__init__.py python","__init__.py python 3","Accenture interview questions and answers","Applied Materials interview questions and answers","Atos interview questions and answers","Capgemini interview questions and answers","CASTING NETWORKS INDIA PVT LIMITED interview questions and answers","CGI Group Inc interview questions and answers","Chetu interview questions and answers","Collabera Technologies interview questions and answers","def __init__(self)","Dell International Services India Pvt Ltd interview questions and answers","Flipkart interview questions and answers","Genpact interview questions and answers","IBM interview questions and answers","Indecomm Global Services interview questions and answers","Infosys Technologies interview questions and answers","L&amp;T Infotech interview questions and answers","Mavenir interview questions and answers","Mphasis interview questions and answers","NetApp interview questions and answers","Oracle Corporation interview questions and answers","PeopleStrong interview questions and answers","Persistent Systems interview questions and answers","Prokarma Softech Pvt Ltd interview questions and answers","python class definition","python class definition example","python class init","python class init function","python class init method","python class init super","python class initialization","python class without __init__","python data science interview questions","python do classes need init","python init not called","python interview questions and answers for testers","python programming questions","python telephonic interview questions","Reliance Industries Ltd interview questions and answers","SAP Labs India Pvt Ltd interview questions and answers","Tech Mahindra interview questions and answers","UnitedHealth Group interview questions and answers","Virtusa Consulting Services Pvt Ltd interview questions and answers","Wells Fargo interview questions and answers","what is python class init ?","Xoriant Soluti interview questions and answers"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/","name":"What is init function in Python ? - Basic Python Interview Questions","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-18T14:00:00+00:00","dateModified":"2021-09-09T11:27:15+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"What is init function in Python ? - basic python interview questions ? - In Python, __init__() method within a class is its constructor.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/python\/what-is-init-function-in-python\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=856"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/856\/revisions"}],"predecessor-version":[{"id":3245,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/856\/revisions\/3245"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}