Classes in Python

Introduction to Classes

What Are Classes?

  • Classes are a core concept in object-oriented programming (OOP).
  • They allow the creation of custom data types that combine attributes (data) and methods (functionality).

Key Benefits

  • Model real-world entities.
  • Promote code reusability.
  • Enhance organization and modularity of programs.

Overview

  • A class serves as a blueprint for creating objects, encapsulating shared attributes and behaviors.

Defining a Class

Basic Syntax

  • Use the class keyword followed by the class name (CamelCase convention).
class Text:
    pass

# Creating an instance of the Text class
my_text = Text()
print(my_text)  # Output: <__main__.Text object at 0x...>
<__main__.Text object at 0x108bd43e0>

Explanation

  • Defined a simple class named Text and created an instance stored in my_text.

Adding Attributes and Methods

Attributes and Methods

  • Attributes are variables belonging to the class.
  • Methods are functions defined within the class that manipulate attributes or perform actions.

Initializing Attributes

  • Use the __init__() method to initialize attributes during object creation.

Example with Methods

class Text:
    def __init__(self, content):
        self.content = content  # Instance attribute
    
    def word_count(self):  # Method
        return len(self.content.split())

    def shout(self):  # Another method
        result = self.content.upper()
        result = result.replace(".", "!")
        return result

my_text = Text("Hello, World! This is a test.")
print(my_text.word_count())  # Output: 6
print(my_text.shout())  # Output: HELLO, WORLD! THIS IS A TEST!
6
HELLO, WORLD! THIS IS A TEST!

Accessing Attributes and Methods

Accessing Attributes

  • Use dot notation to access attributes and methods of a class instance.
print(my_text.content)  # Output: Hello, World! This is a test.
Hello, World! This is a test.

Explanation

  • Accessed the content attribute of my_text using dot notation.

Class vs. Instance Attributes

Class vs. Instance Attributes

  • Instance attributes are defined in __init__() and belong to a specific instance.
  • Class attributes are shared by all instances and defined directly in the class body.

Example:

class Text:
    language = "English"  # Class attribute

    def __init__(self, content):
        self.content = content  # Instance attribute

Accessing Class Attributes

text1 = Text("Hello, World!")
text2 = Text("Bonjour, le monde!")

print(text1.language)  # Output: English
print(text2.language)  # Output: English
English
English

Changing Class Attributes

Changing Class Attributes

  • Modifying a class attribute affects all instances.
Text.language = "French"
print(text1.language)  # Output: French
French

Explanation

  • Changing Text.language affects all instances of the class.

Inheritance

What Is Inheritance?

  • Inheritance allows creating a new class that inherits attributes and methods from an existing class.
  • Promotes code reuse and enables class hierarchies.

Example:

class Text:
    def __init__(self, content):
        self.content = content

class FormattedText(Text):  # Derived class
    def __init__(self, content, format_type):
        super().__init__(content)  # Call the parent class's constructor

Extending Functionality

    def display(self):
        return f"[{self.format_type}] {self.content}"

# Creating instances
my_text = Text("Hello, World!")
formatted_text = FormattedText("Hello, World!", "Bold")

print(my_text.word_count())  # Output: 2
print(formatted_text.display())  # Output: [Bold] Hello, World!

Key Takeaways

  • Classes encapsulate data and functionality.
  • Attributes can be instance-specific or shared across instances.
  • Inheritance enables the creation of new classes based on existing ones, promoting reusability.