{"id":103,"date":"2026-03-02T08:06:09","date_gmt":"2026-03-02T03:06:09","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=103"},"modified":"2026-03-15T17:02:58","modified_gmt":"2026-03-15T12:02:58","slug":"constructors","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/constructors\/","title":{"rendered":"Constructors"},"content":{"rendered":"\n<p>A constructor is a special method in a class that runs automatically when an object is created.<\/p>\n\n\n\n<p>In Python, the constructor method is called <code>__init__()<\/code>.<\/p>\n\n\n\n<p>It is used to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initialize object data<\/li>\n\n\n\n<li>Assign values to attributes<\/li>\n\n\n\n<li>Set up the object when it is created<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>BASIC SYNTAX<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class ClassName:<br>    def __init__(self):<br>        # initialization code<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>SIMPLE EXAMPLE<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self):<br>        print(\"Constructor is called.\")s1 = Student()<\/pre>\n\n\n\n<p>When the object <code>s1<\/code> is created, the constructor runs automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>CONSTRUCTOR WITH PARAMETERS<\/strong><\/h2>\n\n\n\n<p>Most constructors accept parameters to initialize object attributes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name, age):<br>        self.name = name<br>        self.age = ages1 = Student(\"Hira\", 22)print(s1.name)<br>print(s1.age)<\/pre>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__init__<\/code> is the constructor<\/li>\n\n\n\n<li><code>self<\/code> refers to the current object<\/li>\n\n\n\n<li><code>name<\/code> and <code>age<\/code> are object attributes<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>DEFAULT CONSTRUCTOR<\/strong><\/h2>\n\n\n\n<p>If you do not define a constructor, Python automatically provides a default constructor that does nothing.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    passcar1 = Car()<\/pre>\n\n\n\n<p>This works even without <code>__init__()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>TYPES OF CONSTRUCTORS<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Default Constructor<\/h3>\n\n\n\n<p>No parameters except <code>self<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Example:<br>    def __init__(self):<br>        print(\"Default constructor\")<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Parameterized Constructor<\/h3>\n\n\n\n<p>Accepts arguments.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Example:<br>    def __init__(self, value):<br>        self.value = value<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MULTIPLE OBJECTS WITH CONSTRUCTOR<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">class Car:<br>    def __init__(self, brand):<br>        self.brand = brandcar1 = Car(\"Toyota\")<br>car2 = Car(\"BMW\")print(car1.brand)<br>print(car2.brand)<\/pre>\n\n\n\n<p>Each object has its own data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h2>\n\n\n\n<p>\u2022 Constructor name must be <code>__init__<\/code><br>\u2022 It runs automatically when object is created<br>\u2022 Used to initialize object attributes<br>\u2022 <code>self<\/code> is required as the first parameter<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAY<\/strong><\/h2>\n\n\n\n<p>A constructor initializes the object when it is created.<br>It helps assign values and prepare the object for use in a clean and structured way.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Constructors<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773575793698\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":48,"template":"","class_list":["post-103","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constructors - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/constructors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructors - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/constructors\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T12:02:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/constructors\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/constructors\\\/\",\"name\":\"Constructors - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:06:09+00:00\",\"dateModified\":\"2026-03-15T12:02:58+00:00\",\"description\":\"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/constructors\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/constructors\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/constructors\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Constructors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructors - One Language. Endless Possibilities","description":"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.","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:\/\/gigz.pk\/python\/lesson\/constructors\/","og_locale":"en_US","og_type":"article","og_title":"Constructors - One Language. Endless Possibilities","og_description":"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/constructors\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T12:02:58+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/constructors\/","url":"https:\/\/gigz.pk\/python\/lesson\/constructors\/","name":"Constructors - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:06:09+00:00","dateModified":"2026-03-15T12:02:58+00:00","description":"Learn Python constructors and the init method to initialize objects, assign attributes, and structure OOP programs efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/constructors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/constructors\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/constructors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > OOP Basics > Constructors"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}