Objects

Introduction

Objects are one of the core concepts of Object-Oriented Programming (OOP). An object is an instance of a class that contains data and behaviors defined by that class.

In Python, classes act as blueprints, while objects are the actual entities created from those blueprints.

Objects are widely used in:

  • Selenium Automation Frameworks

  • API Automation Frameworks

  • Web Applications

  • Desktop Applications

  • Game Development

  • Data Processing Systems

  • Enterprise Software

Understanding objects is essential because most Python programs interact with objects in one way or another.

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


What is an Object?

An object is an instance of a class.

A class defines the structure and behavior, while an object represents a real-world entity created using that structure.

Example

Consider a class named Student:

class Student:
    pass

Creating an object:

student1 = Student()

Here, student1 is an object of the Student class.


Why Use Objects?

Objects provide:

  • Code reusability

  • Better organization

  • Real-world modeling

  • Easier maintenance

  • Data encapsulation

  • Modular programming


Creating an Object

Objects are created by calling a class.

Syntax

object_name = ClassName()

Example

class Employee:
    pass

employee1 = Employee()

employee1 is an object of the Employee class.


Creating Multiple Objects

A class can create multiple objects.

Example

class Student:
    pass

student1 = Student()
student2 = Student()
student3 = Student()

Each object is independent.


Object with Attributes

Objects can store data through attributes.

Example

class Student:

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

student1 = Student("John")

print(student1.name)

Output

John

The object stores the value of name.


Multiple Objects with Different Data

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.


Accessing Object Attributes

Use dot notation (.).

Example

class Employee:

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

employee = Employee("Mike")

print(employee.name)

Output

Mike

Modifying Object Attributes

Object attributes can be updated.

Example

class Employee:

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

employee = Employee("Mike")

employee.name = "John"

print(employee.name)

Output

John

Object Methods

Objects can call methods defined inside a class.

Example

class Student:

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

student1 = Student()

student1.greet()

Output

Welcome Student

Object 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

Understanding Object Identity

Each object occupies a separate memory location.

Example

class Student:
    pass

student1 = Student()
student2 = Student()

print(student1)
print(student2)

Output

<__main__.Student object at ...>
<__main__.Student object at ...>

The memory addresses differ because they are separate objects.


Real-World Example: Employee Object

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 Object

Example

class Product:

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

product1 = Product(
    "Laptop",
    50000
)

print(product1.name)

Output

Laptop

Objects in Selenium Automation

Objects are heavily used in Selenium frameworks.


Example: Login Page Object

class LoginPage:

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

login = LoginPage()

login.enter_username()

Output

Username Entered

Example: Dashboard Object

class DashboardPage:

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

dashboard = DashboardPage()

dashboard.open_dashboard()

Output

Dashboard Opened

Objects in API Automation

Example

class APIClient:

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

client = APIClient()

client.send_request()

Output

Request Sent

Real-World Example: Bank Account Object

Example

class BankAccount:

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

account = BankAccount("John")

print(account.account_holder)

Output

John

Common Mistakes Beginners Make

Forgetting Parentheses During Object Creation

Incorrect

student = Student

Correct

student = Student()

Accessing Undefined Attributes

Incorrect

class Student:
    pass

student = Student()

print(student.name)

Error

AttributeError

Correct

class Student:

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

Forgetting self

Incorrect

class Student:

    def greet():
        print("Hello")

Error

TypeError

Correct

class Student:

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

Best Practices

Create Meaningful Object Names

employee = Employee()

Instead of:

x = Employee()

Initialize Required Data in init()

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

Use Objects to Model Real-World Entities

Examples:

  • Employee

  • Student

  • Product

  • Customer


Keep Methods Focused

Each method should perform one task.


Reuse Objects When Appropriate

Avoid unnecessary object creation.


Advantages of Objects

  • Better code organization

  • Real-world representation

  • Reusable code

  • Easy maintenance

  • Supports Object-Oriented Programming

  • Improves scalability


Limitations of Objects

  • Slightly more complex than procedural programming

  • Requires understanding of OOP concepts

  • Poor design may increase complexity


Conclusion

Objects are the foundation of Object-Oriented Programming in Python. They represent real-world entities and allow developers to create organized, reusable, and maintainable code.

Whether you’re building Selenium automation frameworks, API testing projects, web applications, or enterprise software, objects help structure your programs efficiently.

Understanding objects is essential before moving on to advanced OOP concepts such as inheritance, polymorphism, encapsulation, and abstraction.


Frequently Asked Questions (FAQs)

What is an object in Python?

An object is an instance of a class.

Example:

student = Student()

What is the difference between a class and an object?

A class is a blueprint, while an object is an actual instance created from that blueprint.


How do I create an object?

object_name = ClassName()

Example:

student = Student()

Can multiple objects be created from one class?

Yes.

student1 = Student()
student2 = Student()

How do I access object attributes?

Use dot notation.

print(student.name)

Key Takeaways

  • Objects are instances of classes.

  • Classes act as blueprints for objects.

  • Objects store data and perform actions.

  • Use parentheses () to create objects.

  • Object attributes are accessed using dot notation.

  • Objects can call class methods.

  • Multiple objects can be created from the same class.

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

  • Proper object design improves code organization and reusability.

  • Understanding objects is essential for mastering Object-Oriented Programming in Python.