Classes

Introduction

Classes are one of the most important concepts in Object-Oriented Programming (OOP). A class acts as a blueprint for creating objects. It allows developers to organize data and functionality into reusable structures.

Object-Oriented Programming helps create modular, maintainable, and scalable applications. Classes are widely used in:

  • Selenium Automation Frameworks

  • API Automation Frameworks

  • Web Development

  • Desktop Applications

  • Game Development

  • Enterprise Software

  • Data Processing Applications

In Python, classes allow you to combine variables (attributes) and functions (methods) into a single unit.

In this tutorial, you will learn what classes are, how to create them, practical examples, automation testing use cases, common mistakes, and best practices.


What is a Class?

A class is a blueprint used to create objects.

Think of a class as a template that defines the properties and behaviors of an object.

Example

A “Car” class may define:

  • Color

  • Brand

  • Speed

And actions such as:

  • Start

  • Stop

  • Accelerate

The actual cars created from the class are called objects.


Why Use Classes?

Benefits of classes include:

  • Code reusability

  • Better organization

  • Easier maintenance

  • Improved scalability

  • Supports Object-Oriented Programming

  • Real-world modeling


Creating a Class

Use the class keyword.

Syntax

class ClassName:
    pass

Example

class Student:
    pass

The class is created but currently contains no data or functionality.


Creating an Object

An object is created from a class.

Example

class Student:
    pass

student1 = Student()

print(student1)

Output

<__main__.Student object at 0x000001>

The object is an instance of the Student class.


Class with Attributes

Attributes are variables that belong to a class.

Example

class Student:
    name = "John"
    age = 25

student1 = Student()

print(student1.name)
print(student1.age)

Output

John
25

Understanding Attributes

Example

class Employee:
    company = "ABC Technologies"

employee1 = Employee()

print(employee1.company)

Output

ABC Technologies

Attributes store information about objects.


The init() Method

The __init__() method is a special constructor method that runs automatically when an object is created.

Example

class Student:

    def __init__(self):
        print("Object Created")

student1 = Student()

Output

Object Created

Creating Instance Attributes

Instance attributes store data unique to each object.

Example

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

student1 = Student("John", 25)

print(student1.name)
print(student1.age)

Output

John
25

Understanding self

self refers to the current object.

Example

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("David")

print(student1.name)

Output

David

Without self, object attributes cannot be accessed properly.


Adding Methods to a Class

Methods are functions inside a class.

Example

class Student:

    def greet(self):
        print("Welcome Student")

student1 = Student()

student1.greet()

Output

Welcome Student

Class with Attributes and Methods

Example

class Student:

    def __init__(self, name):
        self.name = name

    def display(self):
        print(self.name)

student1 = Student("John")

student1.display()

Output

John

Creating Multiple Objects

Example

class Student:

    def __init__(self, name):
        self.name = name

student1 = Student("John")
student2 = Student("David")

print(student1.name)
print(student2.name)

Output

John
David

Each object maintains its own data.


Real-World Example: Employee Class

Example

class Employee:

    def __init__(self, name, department):
        self.name = name
        self.department = department

employee1 = Employee(
    "John",
    "IT"
)

print(employee1.name)
print(employee1.department)

Output

John
IT

Real-World Example: Product Class

Example

class Product:

    def __init__(self, name, price):
        self.name = name
        self.price = price

product1 = Product(
    "Laptop",
    50000
)

print(product1.name)

Output

Laptop

Classes in Selenium Automation

Classes are heavily used in automation frameworks.


Example: Login Page Class

class LoginPage:

    def enter_username(self):
        print("Username Entered")

    def enter_password(self):
        print("Password Entered")

login = LoginPage()

login.enter_username()

Output

Username Entered

Example: Dashboard Page

class DashboardPage:

    def open_dashboard(self):
        print("Dashboard Opened")

dashboard = DashboardPage()

dashboard.open_dashboard()

Output

Dashboard Opened

Classes in API Automation

Example

class APIClient:

    def send_request(self):
        print("API Request Sent")

client = APIClient()

client.send_request()

Output

API Request Sent

Common Mistakes Beginners Make

Forgetting self

Incorrect

class Student:

    def greet():
        print("Hello")

Error

TypeError

Correct

class Student:

    def greet(self):
        print("Hello")

Forgetting Parentheses During Object Creation

Incorrect

student = Student

Correct

student = Student()

Using Undefined Attributes

Incorrect

class Student:
    pass

student = Student()

print(student.name)

Error

AttributeError

Correct

class Student:

    def __init__(self):
        self.name = "John"

Best Practices

Use Meaningful Class Names

class Employee:
    pass

Instead of:

class A:
    pass

Use init() for Initialization

def __init__(self):
    pass

Keep Methods Focused

Each method should perform one task.


Follow Naming Conventions

Use PascalCase for class names.

class LoginPage:
    pass

Reuse Classes

Create reusable classes to reduce duplication.


Advantages of Classes

  • Code reusability

  • Better organization

  • Easy maintenance

  • Supports modular programming

  • Real-world modeling

  • Foundation of OOP


Limitations of Classes

  • Slightly more complex than procedural programming

  • Requires understanding of OOP concepts

  • Poor design can increase complexity


Conclusion

Classes are the foundation of Object-Oriented Programming in Python. They allow developers to group data and functionality into reusable, organized structures.

Whether you’re building Selenium automation frameworks, API testing solutions, web applications, or enterprise software, classes help create scalable and maintainable code.

Understanding classes is essential before learning advanced OOP concepts such as objects, inheritance, polymorphism, encapsulation, and abstraction.


Frequently Asked Questions (FAQs)

What is a class in Python?

A class is a blueprint used to create objects.

Example:

class Student:
    pass

What is an object?

An object is an instance of a class.

Example:

student = Student()

What is the purpose of init()?

The __init__() method initializes object attributes when an object is created.


What is self in Python?

self refers to the current object and is used to access object attributes and methods.


Can a class have multiple methods?

Yes.

Example:

class Student:

    def greet(self):
        pass

    def display(self):
        pass

Key Takeaways

  • Classes are blueprints for creating objects.

  • Objects are instances of classes.

  • Use the class keyword to create classes.

  • The __init__() method initializes object data.

  • self refers to the current object.

  • Methods are functions defined inside classes.

  • Classes improve code organization and reusability.

  • Classes are widely used in Selenium and API automation frameworks.

  • Follow naming conventions and best practices.

  • Classes form the foundation of Object-Oriented Programming in Python.