<title>Python Classes: The Power of Object-Oriented Programming</title>
<id>https://realpython.com/python-classes/</id>
<link href="https://realpython.com/python-classes/"/>
<updated>2026-04-01T14:00:00+00:00</updated>
<summary>Learn how to define and use Python classes to implement object-oriented programming. Dive into attributes, methods, inheritance, and more.</summary>
<content type="html">
<div><p>Python classes are blueprints for creating objects that bundle data and behavior together. Using the <code>class</code> keyword, you define <strong>attributes</strong> to store state and <strong>methods</strong> to implement behavior, then create as many instances as you need. Classes are the foundation of object-oriented programming (OOP) in Python and help you write organized, reusable, and maintainable code.</p>
<p><strong>By the end of this tutorial, you’ll understand that:</strong></p>
<ul>
<li>A <strong>Python class</strong> is a reusable blueprint that defines object attributes and methods.</li>
<li><strong>Instance attributes</strong> hold data unique to each object, while <strong>class attributes</strong> are shared across all instances.</li>
<li>Python classes support <strong>single and multiple inheritance</strong>, enabling code reuse through class hierarchies.</li>
<li><strong>Abstract base classes (ABCs)</strong> define formal interfaces that subclasses must implement.</li>
<li>Classes enable <strong>polymorphism</strong>, allowing you to use different object types interchangeably through shared interfaces.</li>
</ul>
<p>To get the most out of this tutorial, you should be familiar with Python <a href="https://realpython.com/python-variables/">variables</a>, <a href="https://realpython.com/python-data-types/">data types</a>, and <a href="https://realpython.com/defining-your-own-python-function/">functions</a>. Some experience with <a href="https://realpython.com/python3-object-oriented-programming/">object-oriented programming (OOP)</a> is a plus, but you’ll cover all the key concepts you need here.</p>
<div class="alert alert-warning" role="alert">
<p><strong markdown>Get Your Code:</strong> <a href="https://realpython.com/bonus/python-classes-code/" class="alert-link" data-toggle="modal" data-target="#modal-python-classes-code" markdown>Click here to download your free sample code</a> that shows you how to build powerful object blueprints with classes in Python.</p>
</div>
<div class="container border rounded text-wrap-pretty my-3">
<p class="my-3"><mark class="marker-highlight"><strong><span class="icon baseline" aria-hidden="true"><svg aria-hidden="true"><use href="/static/icons.1fb5b1968c3f.svg#@quiz"></use></svg></span> Take the Quiz:</strong></mark> Test your knowledge with our interactive “Python Classes - The Power of Object-Oriented Programming” quiz. You’ll receive a score upon completion to help you track your learning progress:</p>
<hr>
<div class="row my-3">
<div class="col-xs-12 col-sm-4 col-md-3 align-self-center">
<a href="/quizzes/python-classes-oop/" tabindex="-1">
<div class="embed-responsive embed-responsive-16by9">
<img class="card-img-top m-0 p-0 embed-responsive-item rounded" style="object-fit: contain; background: #aadfe5;" alt="Python Classes: The Power of Object-Oriented Programming" src="https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg" width="1920" height="1080" srcset="/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Class-Concepts-Object-Oriented-Programming-in-Python_Watermarked.6cf327c51434.jpg 1920w" sizes="(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)">
<div class="card-img-overlay d-flex align-items-center">
<div class="mx-auto">
<span class="text-light" style="opacity: 0.90;"><span class="icon baseline scale2x" aria-hidden="true"><svg aria-hidden="true"><use href="/static/icons.1fb5b1968c3f.svg#@quiz"></use></svg></span></span>
</div>
</div>
</div>
</a>
</div>
<div class="col">
<div class="mt-3 d-md-none"></div>
<p class="small text-muted mb-0"><strong>Interactive Quiz</strong></p>
<a href="/quizzes/python-classes-oop/" class="stretched-link"><span class="my-0 h4">Python Classes - The Power of Object-Oriented Programming</span></a>
<p class="text-muted mb-0 small">In this quiz, you'll test your understanding of Python classes, including attributes, methods, inheritance, and object-oriented programming concepts.</p>
</div>
</div>
</div>
<h2 id="getting-started-with-python-classes">Getting Started With Python Classes<a class="headerlink" href="#getting-started-with-python-classes" title="Permanent link"></a></h2>
<p>Python is a <a href="https://en.wikipedia.org/wiki/Programming_paradigm">multiparadigm</a> programming language that supports <a href="https://realpython.com/python3-object-oriented-programming/">object-oriented programming (OOP)</a> through classes that you can define with the <code>class</code> keyword. You can think of a class as a piece of code that specifies the <strong>data</strong> and <strong>behavior</strong> that represent and model a particular type of object.</p>
<p>What is a class in Python? A common analogy is that a class is like the blueprint for a house. You can use the blueprint to create several houses and even a complete neighborhood. Each concrete house is an <strong>object</strong> or <strong>instance</strong> that’s derived from the blueprint.</p>
<p>Each instance can have its own properties, such as color, owner, and interior design. These properties carry what’s commonly known as the object’s <a href="https://en.wikipedia.org/wiki/State_(computer_science)">state</a>. Instances can also have different behaviors, such as locking the doors and windows, opening the garage door, turning the lights on and off, watering the garden, and more.</p>
<p>In OOP, you commonly use the term <strong>attributes</strong> to refer to the properties or data associated with a specific object of a given class. In Python, attributes are <a href="https://realpython.com/python-variables/">variables</a> defined inside a class with the purpose of storing all the required data for the class to work.</p>
<p>Similarly, you’ll use the term <strong>methods</strong> to refer to the different behaviors that objects will show. Methods are functions that you define within a class. These functions typically operate on or with the attributes of the underlying instance or class. Attributes and methods are collectively referred to as <strong>members</strong> of a class or object.</p>
<p>You can write classes to model the real world. These classes will help you better organize your code and solve complex programming problems.</p>
<p>For example, you can use classes to create objects that emulate people, animals, vehicles, books, buildings, cars, or other objects. You can also model virtual objects, such as a web server, <a href="https://realpython.com/directory-tree-generator-python/#coding-the-high-level-directorytree-class">directory tree</a>, chatbot, file manager, and more.</p>
<p>Finally, you can use classes to build <strong>class hierarchies</strong>. This way, you’ll promote code reuse and remove repetition throughout your codebase.</p>
<p>In this tutorial, you’ll learn a lot about classes and all the cool things that you can do with them. To kick things off, you’ll start by defining your first class in Python. Then you’ll dive into other topics related to instances, attributes, and methods.</p>
<h3 id="defining-a-class-in-python">Defining a Class in Python<a class="headerlink" href="#defining-a-class-in-python" title="Permanent link"></a></h3>
<p>To define a class, you need to use the <code>class</code> keyword followed by the class name and a colon, just like you’d do for other <a href="https://docs.python.org/3/reference/compound_stmts.html">compound statements</a> in Python. Then you must define the class body, which will start at the next indentation level:</p>
<code-block class="mb-3" aria-label="Code block" data-syntax-language="python_syntax">
<div class="codeblock__header codeblock--blue">
<span class="mr-2 noselect" aria-label="Language">Python Syntax</span>
<div class="noselect">
</div>
</div>
<div class="codeblock__contents">
<div class="highlight highlight--with-header"><pre><span></span><code><span class="k">class</span><span class="w"> </span><span class="nc">ClassName</span><span class="p">:</span>
<span class="o"><</span><span class="n">body</span><span class="o">></span>
</code></pre></div>
<button class="codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only" title="Copy to clipboard" aria-label="Copy to clipboard"><span class="icon baseline" aria-hidden="true"><svg aria-hidden="true"><use href="/static/icons.1fb5b1968c3f.svg#@copy"></use></svg></span></button>
</div>
</code-block>
<p>In a class’s body, you can define attributes and methods as needed. As you already learned, attributes are variables that hold the class data, while methods are functions that provide behavior and typically act on the class data.</p>
<div class="alert alert-primary" role="alert">
<p><strong>Note:</strong> In Python, the body of a given class works as a <a href="https://realpython.com/python-namespaces-scope/">namespace</a> where attributes and methods live. You can only access those attributes and methods through the class or its objects.</p>
</div>
<p>As an example of how to define attributes and methods, say that you need a <code>Circle</code> class to model different circles in a drawing application. Initially, your class will have a single attribute to hold the radius. It’ll also have a method to calculate the circle’s area:</p>
<code-block class="mb-3" aria-label="Code block" data-syntax-language="python">
<div class="codeblock__header codeblock--blue">
<span class="mr-2 noselect" aria-label="Language">Python</span>
<span class="mr-2" aria-label="Filename"><code style="color: inherit; background: inherit;">circle.py</code></span>
<div class="noselect">
</div>
</div>
<div class="codeblock__contents">
<div class="highlight highlight--with-header"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">math</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Circle</span><span class="p">:</span>
<span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">radius</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">radius</span> <span class="o">=</span> <span class="n">radius</span>
<span class="k">def</span><span class="w"> </span><span class="nf">calculate_area</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">radius</span> <span class="o">**</span> <span class="mi">2</span>
</code></pre></div>
<button class="codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only" title="Copy to clipboard" aria-label="Copy to clipboard"><span class="icon baseline" aria-hidden="true"><svg aria-hidden="true"><use href="/static/icons.1fb5b1968c3f.svg#@copy"></use></svg></span></button>
</div>
</code-block>
<p>In this code snippet, you define <code>Circle</code> using the <code>class</code> keyword. Inside the class, you write two methods. The <code>.__init__()</code> method has a special meaning in Python classes. This method is known as the object <a href="https://realpython.com/python-class-constructor/#object-initialization-with-__init__">initializer</a> because it defines and sets the initial values for the object’s attributes. You’ll learn more about this method in the <a href="#instance-attributes">Instance Attributes</a> section.</p>
<p>The second method of <code>Circle</code> is conveniently named <code>.calculate_area()</code> and will compute the area of a specific circle by using its radius. In this example, you’ve used the <a href="https://realpython.com/python-math-module/"><code>math</code></a> module to access the <code>pi</code> constant as it’s defined in that module.</p>
</div><h2><a href="https://realpython.com/python-classes/?utm_source=realpython&utm_medium=rss">Read the full article at https://realpython.com/python-classes/ »</a></h2>
<hr />
<p><em>[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. <a href="https://realpython.com/python-tricks/?utm_source=realpython&utm_medium=rss&utm_campaign=footer">>> Click here to learn more and see examples</a> ]</em></p>
</content>