Topic 2: Creating Classes and Objects

1. Introduction

At the heart of OOP are classes and objects. A class defines a blueprint or prototype from which individual objects are created. The class encapsulates data for the object and methods to manipulate that data.

2. Defining a Class

In Python, a class is defined using the class keyword. The naming convention is to use CamelCase notation for class names.

Example:

python
class Student: pass

This is the simplest form of a class, which does nothing and is just a placeholder.

3. Initializing Objects with Constructors

The __init__ method is a special method called a constructor, automatically called when you create an object from a class. It is used to initialize the attributes of an object.

Example:

python
class Student: def __init__(self, name, age): self.name = name self.age = age

Here, name and age are attributes of the Student class. The self parameter refers to the object itself.

4. Creating Objects

An object is an instance of a class. To create an object, you call the class as if it were a function.

Example:

python
# Creating an object of Student class student1 = Student("Alice", 20) print(student1.name) # Outputs: Alice print(student1.age) # Outputs: 20

5. Adding Methods to a Class

A method is a function defined inside a class and is used to define behaviors of an object.

Example:

python
class Student: def __init__(self, name, age): self.name = name self.age = age def introduce(self): return f"My name is {self.name} and I am {self.age} years old." student1 = Student("Bob", 22) print(student1.introduce()) # Outputs: My name is Bob and I am 22 years old.

6. Modifying Object Attributes

Object attributes can be modified directly or via methods defined in the class.

Example:

python
class Student: # ... [previous code] def set_age(self, age): self.age = age student1 = Student("Charlie", 18) print(student1.age) # Outputs: 18 student1.set_age(19) print(student1.age) # Outputs: 19

7. Deleting Objects and Attributes

You can delete objects and their attributes using the del keyword.

Example:

python
student2 = Student("David", 21) print(student2.age) # Outputs: 21 # Delete the age attribute del student2.age # Delete the student2 object del student2

8. The __str__ Method

This method allows us to define a user-friendly or informative representation for our object, which gets called with the print() function or str().

Example:

python
class Student: # ... [previous code] def __str__(self): return f"Student(name={self.name}, age={self.age})" student3 = Student("Eva", 23) print(student3) # Outputs: Student(name=Eva, age=23)

9. Conclusion

Classes provide a means of bundling data and methods operating on that data within one unit. By understanding how to create classes and objects, you set the foundation for more advanced OOP principles like inheritance, polymorphism, and encapsulation. The ability to define and manipulate your own data types via classes is a powerful tool in a programmer’s toolkit.