{"id":75469,"date":"2020-01-24T10:49:33","date_gmt":"2020-01-24T05:19:33","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75469"},"modified":"2024-08-22T18:05:44","modified_gmt":"2024-08-22T12:35:44","slug":"python-classes","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-classes\/","title":{"rendered":"Python Classes &#8211; Learn Object-Oriented Programming in Python"},"content":{"rendered":"<p>In this article, we\u2019ll explore the fundamentals of object-oriented programming (OOP). OOP is a programming model that organizes software design around data, or objects, rather than functions and logic. This approach allows for more modular, reusable, and flexible code. An object can be defined as a data field that possesses unique attributes and behavior, encapsulating both data and methods that operate on the data.<\/p>\n<p>Today, we will delve into classes in Python, which are the blueprints for creating objects. By understanding how to define and use classes, you&#8217;ll gain the skills to build more complex and organized Python programs.<\/p>\n<h3>Python Classes<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-classes.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-75582 aligncenter\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/python-classes.jpg\" alt=\"classes in python\" width=\"802\" height=\"420\" \/><\/a><\/h3>\n<p><strong>Python<\/strong> is an <strong>object-oriented language<\/strong> and everything in it is an <strong>object<\/strong>. By using Python <strong>classes<\/strong> and <strong>objects<\/strong>, we can <strong>model<\/strong> the <strong>real world<\/strong>.<\/p>\n<p>A <strong>class<\/strong> is like a <strong>blueprint<\/strong> for <strong>objects<\/strong> &#8211; it has <strong>no values<\/strong> itself. It is an <strong>abstract data type<\/strong>. We can have <strong>multiple objects<\/strong> of <strong>one class<\/strong>.<\/p>\n<h3>Defining a Class in Python<\/h3>\n<p>To define a <strong>class<\/strong>, we use the <strong>class keyword<\/strong>, <strong>not<\/strong> the <strong>def keyword<\/strong> we used to <strong>define functions<\/strong>.<\/p>\n<p>Let\u2019s take an <strong>example<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  pass<\/pre>\n<p>This was an <strong>empty class<\/strong>.<\/p>\n<p>Now let\u2019s declare a <strong>variable<\/strong> in it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  name=\u2019fruit\u2019<\/pre>\n<p>This is a <strong>class Fruit<\/strong>.<\/p>\n<p>We use <strong>PascalCase<\/strong> to <strong>name classes<\/strong> here. And because <strong>Python does not<\/strong> use <strong>curly braces<\/strong>, you need to <strong>indent<\/strong> the <strong>block of code<\/strong> in the <strong>class<\/strong>. Without that, it will <strong>raise <\/strong>an <strong>exception<\/strong>.<\/p>\n<h3>Creating an Object in Python<\/h3>\n<p>There is <strong>no<\/strong> need to <strong>use<\/strong> the <strong>new keyword<\/strong> for <strong>creating<\/strong> objects in <strong>Python<\/strong>. Just use this <strong>class constructor<\/strong>:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit()\r\n&gt;&gt;&gt; orange.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u2018Fruit\u2019<\/div>\n<p>This calls the <strong>__init__()<\/strong> method in the <strong>Fruit class<\/strong>.<\/p>\n<p>We <strong>did not<\/strong> declare it, but it uses the <strong>default one<\/strong>.<\/p>\n<p><strong>Python classes<\/strong> can also have <strong>docstrings<\/strong> to explain what they do. But if you <strong>add<\/strong> a <strong>docstring<\/strong> to a class, it should be the <strong>first thing<\/strong> in the <strong>class<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class MyClass:\r\n  '''This class does nothing'''\r\n  pass<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; MyClass\r\n&lt;class '__main__.MyClass'&gt;<\/pre>\n<h3>The __init__() Method<\/h3>\n<p>So what is<strong> __init__()<\/strong>?<\/p>\n<p>In most cases, we will need the __init__() method for <strong>classes<\/strong>.<\/p>\n<p>This is a <strong>magic method (dunder method)<\/strong> which we can use to <strong>initialize values<\/strong> for <strong>classes (objects)<\/strong>. So __init__() has <strong>initialization code<\/strong>.<\/p>\n<p>Every <strong>class<\/strong> has <strong>__init__<\/strong> and this is <strong>executed<\/strong> when we <strong>instantiate<\/strong> the <strong>class<\/strong>. You can also use this method to do <strong>anything<\/strong> you want to do when the <strong>object<\/strong> is being <strong>created<\/strong>.<\/p>\n<p>Let\u2019s learn with an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  def __init__(self, color, size):\r\n    self.name='fruit'\r\n    self.color=color\r\n    self.size=size<\/pre>\n<h4>1. Accessing Values<\/h4>\n<p>We have <strong>created<\/strong> the <strong>class<\/strong>, now let\u2019s learn to create an <strong>object<\/strong> for it and <strong>access<\/strong> its <strong>values<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange',8)\r\n&gt;&gt;&gt; orange.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;fruit&#8217;<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange.color<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;orange&#8217;<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange.size<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<p>Here, class Fruit has an <strong>object orange<\/strong>. This has a <strong>name<\/strong>, <strong>color<\/strong>, and <strong>size<\/strong>.<\/p>\n<p>The name for all fruits is <strong>name<\/strong>, and <strong>color<\/strong> and <strong>size<\/strong> are passed as <strong>arguments<\/strong>. So <strong>orange<\/strong> is the object with color <strong>\u2018orange\u2019<\/strong> and<strong> size 8<\/strong>. We <strong>accessed<\/strong> the <strong>values<\/strong> with the <strong>dot operator<\/strong>.<\/p>\n<p>We will talk about the <strong>\u2018self\u2019 parameter<\/strong> in the next section.<\/p>\n<h3>Python Classes Methods<\/h3>\n<p>Python classes can have both methods and functions. Functions are standalone and not associated with an object. However, methods are functions that belong to an object and can modify its state. Each method in a class must take the self parameter as the first parameter. This parameter refers to the instance of the class, allowing the method to access and modify the object\u2019s attributes. You can name this parameter anything you like, but by convention, it is always named self.<\/p>\n<p>Let\u2019s define a method in <strong>class Fruit<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  def __init__(self, color, size):\r\n    self.name='fruit'\r\n    self.color=color\r\n    self.size=size\r\n  def show(self):\r\n    print(f'I am a {self.name}, I am {self.color} and of size {self.size}')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange',8)<\/pre>\n<p>Now, we can call the<strong> show()<\/strong> method on the <strong>orange object<\/strong>. For this, we use the <strong>dot operator<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange.show()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am a fruit, I am orange and of size 8<\/div>\n<p>You can call the <strong>self parameter<\/strong> anything.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  def __init__(obj, color, size):\r\n    obj.name='fruit'\r\n    obj.color=color\r\n    obj.size=size\r\n  def show(obj):\r\n    print(f'I am a {obj.name}, I am {obj.color} and of size {obj.size}')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange',8)\r\n&gt;&gt;&gt; orange.show()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am a fruit, I am orange and of size 8<\/div>\n<p>You also have to <strong>use<\/strong> the <strong>self parameter<\/strong> even when a method <strong>does not<\/strong> take any <strong>parameters<\/strong>.<\/p>\n<h3>Changing Object Properties<\/h3>\n<p>In Python classes, we use the <strong>dot operator<\/strong> to access <strong>object<\/strong> <strong>properties<\/strong> and <strong>methods<\/strong>, but we can also use it to <strong>modify them<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange.size=9\r\n&gt;&gt;&gt; orange.size<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">9<\/div>\n<p>And we can also <strong>delete<\/strong> them with the <strong>del keyword<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del orange.size\r\n&gt;&gt;&gt; orange.size<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<b>\u00a0\u00a0<\/b>File &#8220;&lt;pyshell#33&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<b>\u00a0\u00a0\u00a0\u00a0<\/b>orange.size<br \/>\nAttributeError: &#8216;Fruit&#8217; object has no attribute &#8216;size&#8217;<\/div>\n<p>We can also <strong>delete<\/strong> the <strong>complete<\/strong> object with the <strong>del keyword<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; del orange\r\n&gt;&gt;&gt; orange.size<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400;\">\u00a0\u00a0<\/span>File &#8220;&lt;pyshell#35&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0<\/span>orange.size<br \/>\nNameError: name &#8216;orange&#8217; is not defined<\/div>\n<h3>Python Class Variables<\/h3>\n<p>The Python Classes <strong>create<\/strong> new <strong>local<\/strong> <strong>namespaces<\/strong> with their <strong>attributes (data or functions)<\/strong>. You can make a <strong>variable<\/strong> belong to a <strong>class<\/strong>. This is <strong>not<\/strong> like <strong>instance variables<\/strong> which belong to <strong>objects<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  name='fruit'\r\n  def __init__(obj, color, size):\r\n    obj.color=color\r\n    obj.size=size\r\n  def show(obj):\r\n    print(f'I am a {obj.name}, I am {obj.color} and of size {obj.size}')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange',8)\r\n&gt;&gt;&gt; orange.name<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;fruit&#8217;<\/div>\n<p>Here, the class <strong>Fruit<\/strong> has the <strong>class variable<\/strong> \u2018orange\u2019, but the orange <strong>object<\/strong> also has it.<\/p>\n<h4>1. Built-in Class Attributes<\/h4>\n<p><strong>Python<\/strong> has some <strong>built-in class attributes<\/strong> that we can use to get <strong>more information<\/strong> about the <strong>class<\/strong>.<\/p>\n<p><strong>a. __dict__ &#8211;<\/strong> It is a <strong>dictionary<\/strong> consisting of the <strong>class\u2019 namespace<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.__dict__<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">mappingproxy({&#8216;__module__&#8217;: &#8216;__main__&#8217;, &#8216;__init__&#8217;: &lt;function Fruit.__init__ at 0x0000023450FB0048&gt;, &#8216;show&#8217;: &lt;function Fruit.show at 0x0000023450FB00D0&gt;, &#8216;__dict__&#8217;: &lt;attribute &#8216;__dict__&#8217; of &#8216;Fruit&#8217; objects&gt;, &#8216;__weakref__&#8217;: &lt;attribute &#8216;__weakref__&#8217; of &#8216;Fruit&#8217; objects&gt;, &#8216;__doc__&#8217;: None})<\/div>\n<p><strong>b. __doc__ &#8211;<\/strong> It is the <strong>class docstring<\/strong>, but is <strong>None<\/strong> if there is <strong>no docstring<\/strong> in the <strong>class<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.__doc__<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>&gt;&gt;&gt;<\/p>\n<\/div>\n<p><strong>c. __name__ &#8211;<\/strong> This is the <strong>class name<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.__name__<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;Fruit&#8217;<\/div>\n<p><strong>d. __module__ &#8211;<\/strong> This is the <strong>name<\/strong> of the <strong>module<\/strong> in which the <strong>class is defined<\/strong>.<\/p>\n<p>In the <strong>interactive mode<\/strong>, this is<strong> __main__<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.__module__<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">&#8216;__main__&#8217;<\/div>\n<p><strong>e. __bases__ &#8211;<\/strong> This is a <strong>tuple<\/strong> consisting of the <strong>base classes<\/strong> of this <strong>class<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; Fruit.__bases__<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">(&lt;class &#8216;object&#8217;&gt;,)<\/div>\n<h3>Instance Variables in Python<\/h3>\n<p>In python classes, Instance variables <strong>belong<\/strong> to <strong>objects<\/strong>. We saw an example, but now we will see those<strong> other methods<\/strong> that <strong>__init__<\/strong> can <strong>define<\/strong> them too.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; class Fruit:\r\n  def __init__(obj, color):\r\n    obj.name='fruit'\r\n    obj.color=color\r\n  def show(obj, size):\r\n    obj.size=size\r\n    print(f'I am a {obj.name}, I am {obj.color} and of size {obj.size}')<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; orange=Fruit('orange')\r\n&gt;&gt;&gt; orange.show(8)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">I am a fruit, I am orange and of size 8<\/div>\n<p>Here, we define the <strong>size<\/strong> in the <strong>show()<\/strong> method using <strong>obj.size=size<\/strong> and it <strong>works<\/strong> like it should.<\/p>\n<h3>Summary<\/h3>\n<p>So in this tutorial, we learned about <strong>classes<\/strong> in <strong>Python<\/strong>. We saw how to <strong>define<\/strong> them, <strong>create objects<\/strong>, the<strong> __init__ method<\/strong>, <strong>methods<\/strong>, <strong>changing<\/strong> and <strong>deleting<\/strong> object properties, <strong>class variables<\/strong>, and <strong>instance variables<\/strong>. Next, we will deal with <strong>objects<\/strong> in detail.<\/p>\n<p>Till then, keep practicing python classes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we\u2019ll explore the fundamentals of object-oriented programming (OOP). OOP is a programming model that organizes software design around data, or objects, rather than functions and logic. This approach allows for more&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75582,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1425,1426,1427,1428,1429,1430,1431,1432],"class_list":["post-75469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-changing-object-properties","tag-classes-in-python","tag-creating-an-object-in-python","tag-defining-a-class-in-python","tag-instance-variables-in-python","tag-python-class-variables","tag-python-classes","tag-python-classes-methods"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Classes - Learn Object-Oriented Programming in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn the concept of classes in Python, creating objects, the __init__ method, class variables, and instance variables with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-classes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Classes - Learn Object-Oriented Programming in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn the concept of classes in Python, creating objects, the __init__ method, class variables, and instance variables with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-classes\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-24T05:19:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T12:35:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Classes - Learn Object-Oriented Programming in Python - TechVidvan","description":"Learn the concept of classes in Python, creating objects, the __init__ method, class variables, and instance variables with examples.","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:\/\/techvidvan.com\/tutorials\/python-classes\/","og_locale":"en_US","og_type":"article","og_title":"Python Classes - Learn Object-Oriented Programming in Python - TechVidvan","og_description":"Learn the concept of classes in Python, creating objects, the __init__ method, class variables, and instance variables with examples.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-classes\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-24T05:19:33+00:00","article_modified_time":"2024-08-22T12:35:44+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Classes &#8211; Learn Object-Oriented Programming in Python","datePublished":"2020-01-24T05:19:33+00:00","dateModified":"2024-08-22T12:35:44+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/"},"wordCount":1067,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg","keywords":["Changing Object Properties","classes in python","Creating an Object in Python","Defining a Class in Python","Instance Variables in Python","Python Class Variables","python classes","Python Classes Methods"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-classes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/","url":"https:\/\/techvidvan.com\/tutorials\/python-classes\/","name":"Python Classes - Learn Object-Oriented Programming in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg","datePublished":"2020-01-24T05:19:33+00:00","dateModified":"2024-08-22T12:35:44+00:00","description":"Learn the concept of classes in Python, creating objects, the __init__ method, class variables, and instance variables with examples.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-classes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-classes.jpg","width":802,"height":420,"caption":"classes in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-classes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Classes &#8211; Learn Object-Oriented Programming in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75469"}],"version-history":[{"count":2,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75469\/revisions"}],"predecessor-version":[{"id":447680,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75469\/revisions\/447680"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75582"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}