Python - Objects
super()
function in Python objects?
In Python, the super()
function is used to call a method from the parent class (superclass or base class) within a subclass. It provides a way to invoke the method of the immediate superclass, allowing for code reuse and facilitating method overriding.
Purpose of super()
Function:
- Allows access to methods in the superclass from the subclass.
- Used primarily in the context of method overriding to call the overridden method in the parent class.
- Ensures that the method resolution order (MRO) is followed, providing a consistent way to navigate the class hierarchy.
Let's illustrate the purpose of super()
with a simple Python program:
# Define a base class named 'Vehicle' with a method
class Vehicle:
def start_engine(self):
return "Generic engine start"
# Define a subclass named 'Car' that overrides the 'start_engine' method using 'super()'
class Car(Vehicle):
def start_engine(self):
# Call the overridden method from the base class using super()
base_start = super().start_engine()
return f"Car engine start: {base_start}"
# Create an instance (object) of the 'Car' class
car_instance = Car()
# Call the overridden method in the subclass, which uses 'super()' to call the base method
car_engine_start = car_instance.start_engine()
In this example, we define a base class 'Vehicle' with a method 'start_engine'. We then create a subclass 'Car' that inherits from 'Vehicle' and overrides the 'start_engine' method using 'super()' to call the method from the base class.
We create an instance ('car_instance') of the 'Car' class and call the overridden method, demonstrating the use of 'super()' to invoke the method in the parent class.
Output:
Car engine start: Generic engine start
The key takeaway is that super()
allows a subclass to access and call methods from the parent class, facilitating method overriding and ensuring a consistent method resolution order within the class hierarchy.
__str__
method in Python objects.
In Python, the __str__
method is a special method that provides a human-readable representation of an object. It is called by the built-in str()
function and the print()
function when attempting to convert an object to a string. Overriding __str__
allows customizing the string representation of objects.
Significance of __str__
Method:
- Used to define a user-friendly and informative string representation of an object.
- Called implicitly when using
str()
orprint()
on an object. - Facilitates debugging and improves the readability of code.
Let's illustrate the significance of __str__
with a simple Python program:
# Define a class named 'Book' with a custom __str__ method
class Book:
def __init__(self, title, author, publication_year):
self.title = title
self.author = author
self.publication_year = publication_year
# Override the __str__ method to provide a custom string representation
def __str__(self):
return f"Book: {self.title} by {self.author} ({self.publication_year})"
# Create an instance (object) of the 'Book' class
book_instance = Book(title="The Great Gatsby", author="F. Scott Fitzgerald", publication_year=1925)
# Call the built-in 'str()' function and 'print()' function on the object
string_representation = str(book_instance)
In this example, we define a class 'Book' with a custom __str__
method that returns a formatted string representation of the book. We create an instance ('book_instance') of the 'Book' class and demonstrate how the __str__
method is implicitly called when using str()
or print()
on the object.
Output:
String representation of the book: Book: The Great Gatsby by F. Scott Fitzgerald (1925)
The key takeaway is that overriding the __str__
method allows customization of the string representation of objects, improving code readability and providing essential information about the object's state.
In Python, the comparison of objects for equality is typically done using the ==
operator. However, for custom objects, you may need to override the __eq__
method to define the equality comparison based on the object's attributes.
Comparing Objects for Equality:?
- Use the
==
operator for built-in types and objects with a default__eq__
implementation. - Override the
__eq__
method in custom classes to define custom equality comparison. - The
__eq__
method should returnTrue
if the objects are considered equal, andFalse
otherwise.
Let's illustrate how to compare objects for equality with a simple Python program:
# Define a class named 'Person' with an __eq__ method for custom equality comparison
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Override the __eq__ method to compare objects based on attributes
def __eq__(self, other):
if isinstance(other, Person):
return self.name == other.name and self.age == other.age
return False
# Create instances (objects) of the 'Person' class
person1 = Person(name="Alice", age=25)
person2 = Person(name="Bob", age=30)
person3 = Person(name="Alice", age=25)
# Compare objects for equality using the == operator
are_persons_equal_1 = person1 == person2
are_persons_equal_2 = person1 == person3
In this example, we define a class 'Person' with an __eq__
method that compares objects based on their 'name' and 'age' attributes. We create instances ('person1', 'person2', 'person3') of the 'Person' class and use the ==
operator to compare them for equality.
Output:
Are persons equal 1: False Are persons equal 2: True
The key takeaway is that while the ==
operator is suitable for built-in types and objects with a default __eq__
implementation, custom classes may require overriding the __eq__
method for a more specific definition of equality.
__dict__
attribute in Python objects.
In Python, the __dict__
attribute is a dictionary containing the attributes (instance variables) of an object. It provides a way to access and manipulate the attributes of an object directly. Each key-value pair in the dictionary corresponds to an attribute and its value.
Use of __dict__
Attribute:
- Allows inspection and manipulation of object attributes at runtime.
- Provides a convenient way to access and modify attributes dynamically.
- Useful for debugging and introspection purposes.
Let's illustrate the use of __dict__
with a simple Python program:
# Define a class named 'Person' with attributes
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Create an instance (object) of the 'Person' class
person_instance = Person(name="Alice", age=25)
# Access and modify attributes using the __dict__ attribute
person_attributes_before = person_instance.__dict__
person_instance.name = "Bob" # Modify the 'name' attribute
person_attributes_after = person_instance.__dict__
In this example, we define a class 'Person' with attributes 'name' and 'age'. We create an instance ('person_instance') of the 'Person' class and use the __dict__
attribute to inspect the attributes before and after modifying the 'name' attribute.
Output:
Attributes before modification: {'name': 'Alice', 'age': 25} Attributes after modification: {'name': 'Bob', 'age': 25}
The key takeaway is that the __dict__
attribute allows dynamic inspection and modification of object attributes. While it provides a powerful mechanism, it's essential to use it judiciously, and direct manipulation of attributes might not always be the recommended practice in Python programming.