Python's object-oriented programming (OOP) is a powerful feature that allows developers to create robust, modular, and scalable code. One of the central concepts of OOP is the 'class', which serves as a blueprint for creating objects with certain properties and methods. Let's dive into a practical example of a Python class to illustrate OOP principles.

In this article, we'll create a simple 'Book' class, demonstrating key OOP concepts such as attributes, methods, encapsulation, inheritance, and polymorphism. By the end, we'll have a basic understanding of how Python classes work and how they can be utilized to build efficient and maintainable software.

Defining a Python Class
The 'Book' class will have attributes like 'title', 'author', and 'price', as well as methods like 'display_book' to print the book details. We'll start with a basic class definition:

```python class Book: def __init__(self, title, author, price): self.title = title self.author = author self.price = price ```
Instance Attributes and the __init__ Method

The '__init__' method is a special method in Python classes. It is the constructor that initializes the attributes of the class when a new instance is created. In our Book class, title, author, and price are instance attributes:
```python book1 = Book('Python Programming', 'John Smith', 29.99) print(book1.title) # Output: Python Programming ```
Encapsulation with Private Attributes

Encapsulation is the practice of hiding data (attributes) and the methods that manipulate that data within a class, preventing their direct access from outside the class. In Python, we can achieve this using name mangling with double underscore:
```python class Book: def __init__(self, title, author, price): self.__title = title self.__author = author self.__price = price def display_book(self): print(f'Title: {self.__title}, Author: {self.__author}, Price: {self.__price}') ```
Inheritance: Creating a 'Ebook' Class

Inheritance allows us to create new classes that are based on existing classes, inheriting their attributes and methods. Let's create a 'Ebook' class that inherits from 'Book':
```python class Ebook(Book): def __init__(self, title, author, price, file_format): super().__init__(title, author, price) self.__file_format = file_format def display_ebook(self): super().display_book() print(f'File Format: {self.__file_format}') ```




















Polymorphism: Overriding the display_book Method
Polymorphism means 'many shapes'. In OOP, it refers to the ability to use a common interface for multiple types. In our case, we'll override the 'display_book' method in the 'Ebook' class to include its unique attribute 'file_format':
```python ebook1 = Ebook('Python Programming', 'John Smith', 24.99, 'PDF') ebook1.display_book() # Will display both book and ebook details ```
With this, we've covered the basics of Python classes and OOP principles. To continue learning, explore more about abstract classes, interfaces, and Python's built-in classes. Happy coding!