How to Create a Class in Python (Object-Oriented Programming)

Learn how to create a class in Python. Master the __init__ constructor, instance methods, self keyword, and object instantiation.

Try Code in Editor

Explanation

Object-Oriented Programming (OOP) is a powerful paradigm used to model real-world entities, structure applications, and promote code reusability. In Python, everything is an object, and classes are the blueprints from which individual objects are created. Creating a class allows you to bundle data attributes and functions together.

To define a class, you use the `class` keyword followed by the class name (conventionally written in PascalCase). Inside the class, the most important method is the constructor: `__init__(self, ...)`. This special magic method is automatically executed when you instantiate the class, initializing the object's starting data attributes.

The `self` parameter represents the specific instance of the class you are currently creating or modifying. Every instance method within the class must accept `self` as its first argument. Instance methods are functions defined inside the class that operate on the object's attributes, enabling clean encapsulation of logic.

Step-by-Step Implementation

  1. 1

    Use the class keyword to declare the class template.

  2. 2

    Define the __init__(self, ...) constructor to set up initial attributes.

  3. 3

    Add instance methods that accept self as the first parameter to work with object state.

  4. 4

    Call ClassName(...) to instantiate and create a new object instance.

Code Example

This script demonstrates creating a class, defining properties, class instance methods, and creating objects.

create_class.py
Try in Editor
# 1. Defining a class
class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email
        self.active = True  # Default attribute

    def get_info(self):
        status = "Active" if self.active else "Inactive"
        return f"User: {self.username} | Email: {self.email} | Status: {status}"

# 2. Instantiating objects of the class
user1 = User("codeguy", "guy@example.com")
user2 = User("pygirl", "girl@example.com")

# 3. Accessing attributes and invoking methods
print(user1.get_info())
print(user2.username, "has email", user2.email)
Terminal Output
User: codeguy | Email: guy@example.com | Status: Active
pygirl has email girl@example.com

Frequently Asked Questions

What does the self parameter represent?

It refers to the specific instance of the object being created or manipulated. It is required to bind attributes to the instance.

Can a class have attributes that are shared across all instances?

Yes, these are class attributes. Define them directly under the class header (outside of __init__) and access them using ClassName.attribute.

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.