{"id":105,"date":"2026-03-02T08:14:31","date_gmt":"2026-03-02T03:14:31","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=105"},"modified":"2026-03-15T17:03:02","modified_gmt":"2026-03-15T12:03:02","slug":"instance-vs-class-variables","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/","title":{"rendered":"Instance vs Class Variables"},"content":{"rendered":"\n<p>In Object-Oriented Programming (OOP), variables inside a class are mainly of two types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Instance Variables<\/li>\n\n\n\n<li>Class Variables<\/li>\n<\/ul>\n\n\n\n<p>Understanding the difference is very important for writing clean and structured programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Instance Variables<\/strong><\/h2>\n\n\n\n<p>Instance variables belong to a specific object.<br>Each object has its own separate copy of instance variables.<\/p>\n\n\n\n<p>They are usually defined inside the constructor using <code>self<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    def __init__(self, name):<br>        self.name = name   # Instance variables1 = Student(\"Hira\")<br>s2 = Student(\"Ali\")print(s1.name)<br>print(s2.name)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Hira<br>Ali<\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>s1<\/code> and <code>s2<\/code> have their own separate <code>name<\/code><\/li>\n\n\n\n<li>Changing one object does not affect the other<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Points<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Defined using <code>self<\/code><\/li>\n\n\n\n<li>Created when object is created<\/li>\n\n\n\n<li>Unique for each object<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Class Variables<\/strong><\/h2>\n\n\n\n<p>Class variables belong to the class itself, not to individual objects.<br>All objects share the same class variable.<\/p>\n\n\n\n<p>They are defined inside the class but outside the constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">class Student:<br>    school = \"GIGZ Academy\"   # Class variable    def __init__(self, name):<br>        self.name = names1 = Student(\"Hira\")<br>s2 = Student(\"Ali\")print(s1.school)<br>print(s2.school)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GIGZ Academy<br>GIGZ Academy<\/pre>\n\n\n\n<p>Both objects share the same <code>school<\/code> value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Changing Class Variable<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Student.school = \"W3Skillset\"print(s1.school)<br>print(s2.school)<\/pre>\n\n\n\n<p>Now both objects will show the updated value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Difference Between Instance and Class Variables<\/strong><\/h2>\n\n\n\n<p>Instance Variable<br>Belongs to object<br>Separate copy for each object<br>Defined using <code>self<\/code><br>Created when object is created<\/p>\n\n\n\n<p>Class Variable<br>Belongs to class<br>Shared by all objects<br>Defined directly inside class<br>Created when class is defined<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>When to Use Which?<\/strong><\/h2>\n\n\n\n<p>Use Instance Variables when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data is different for each object<\/li>\n\n\n\n<li>Example: name, age, salary<\/li>\n<\/ul>\n\n\n\n<p>Use Class Variables when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data is common for all objects<\/li>\n\n\n\n<li>Example: school name, company name, tax rate<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Final Understanding<\/strong><\/h2>\n\n\n\n<p>Instance variables store object-specific data.<br>Class variables store shared data for all objects of the class.<\/p>\n\n\n\n<p>Both are essential for writing efficient and well-structured OOP programs.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773576126059\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\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 > Instance vs Class Variables<\/span><\/span><\/div>\n\n\n<p><\/p>\n","protected":false},"menu_order":49,"template":"","class_list":["post-105","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>Instance vs Class Variables - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.\" \/>\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\/instance-vs-class-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Instance vs Class Variables - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-15T12:03:02+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/instance-vs-class-variables\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/instance-vs-class-variables\\\/\",\"name\":\"Instance vs Class Variables - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:14:31+00:00\",\"dateModified\":\"2026-03-15T12:03:02+00:00\",\"description\":\"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/instance-vs-class-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/instance-vs-class-variables\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/instance-vs-class-variables\\\/#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 > Instance vs Class Variables\"}]},{\"@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":"Instance vs Class Variables - One Language. Endless Possibilities","description":"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.","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\/instance-vs-class-variables\/","og_locale":"en_US","og_type":"article","og_title":"Instance vs Class Variables - One Language. Endless Possibilities","og_description":"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.","og_url":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-15T12:03:02+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/","url":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/","name":"Instance vs Class Variables - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:14:31+00:00","dateModified":"2026-03-15T12:03:02+00:00","description":"Learn the difference between instance and class variables in Python OOP, how they work, and when to use them in classes.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/instance-vs-class-variables\/#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 > Instance vs Class Variables"}]},{"@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\/105","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=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}