Intro: Why?
What is the purpose of inheritance ?
Here are some other business cases where inheritance can be used:
A bank can use inheritance to create different types of accounts, such as checking accounts, savings accounts, and investment accounts.
A retail store can use inheritance to create different types of products, such as clothing, electronics, and home goods.
A software company can use inheritance to create different types of applications, such as web applications, mobile applications, and desktop applications.
Inheritance is a powerful tool that can be used to improve the design and readability of our code. In the right hands, it can be a valuable asset to any business.
Multiple inheritance:
Allows a class to inherit behaviour and attributes from more than one parent class. In other words, a subclass can have multiple superclasses.
There was a company called "E-commerce Emporium" that sold a wide range of products, including electronics, clothing, and home décor items. To organize their products efficiently, they decided to create a class hierarchy that captured the common attributes and methods of all their products, as well as the specific attributes and methods of each type of product.
To categorize their products effectively, the company wanted to introduce the concept of product categories. They created a separate class called Category that had an attribute category_name and a method display_category() to display the category information.
Now, the company wanted to introduce a new type of product called OnlineProduct, which inherited from both the Product and Category classes. The OnlineProduct class included an additional attribute discount to represent the discount percentage on the product.
Inheritance Magic Methods: __str__
Note : The expression type(self).__name__ returns the name of the class of the object self. For example, if self is an instance of the class Bard, then type(self).__name__ will return the string "Bard".
Here is an example:
class Bard:
def __init__(self):
print(type(self).__name__)
bard = Bard()
# Output:
# Bard
The expression type(self).__name__ is often used in conjunction with the if statement to check whether an object is an instance of a particular class. For example, the following code will print "Yes" if self is an instance of the class Bard:
Python
if type(self).__name__ == "Bard":
print("Yes")we can use type(self).__name__ with __str__ to return a more informative string representation of an object. For example, the following code defines a class called Book and implements a __str__ method that uses type(self).__name__ to return the name of the class, the title of the book, and the author of the book:
How to list all attributes and methods of a class or instance
In Python, you can use the built-in function dir() to list all attributes and methods of a class or instance. Here's an example: (Note: returns a a list)

dir() returns a list of all attributes and methods of the Person class and the p instance. This can be useful for introspection and debugging purposes.Public instance method use in Inheritance:
here's an example of how to use a public instance method in inheritance in Python:
r = Rectangle(5, 10, 'red')
print(r.area()) # Output: 50We are able to call the area() method on the instances of the Rectangle classe because area() is a public instance method that is inherited from the Shape class.
isinstance versus type vs subclass: 👨🏽🌾👨🏽🌾👨🏽🌾
Check if an object is an instance of a particular class or of a subclass
The isinstance() function is a built-in Python function that is used to determine whether an object is an instance of a particular class or of a subclass of that class. Here's the basic syntax of the isinstance() function:
isinstance(object, class)class MyClass:
pass
obj = MyClass()
We can use the isinstance() function to check if obj is an instance of MyClass:
issubclass(type(obj), a_class) An object is exactly an instance of a specified class
If you question specifically asks to check whether an object is exactly an instance of a specified class, not a subclass of that class :
use type( )
return type(obj) is a_class /* True or False */To test whether an object is an instance of a class that inherited (directly or indirectly) from a specified class, you can use the
issubclass()function:For example, let's say we have a class hierarchy like this:
class MyBaseClass: pass class MySubClass(MyBaseClass): pass class MySubSubClass(MySubClass): passWe can use
issubclass()to check whetherMySubSubClassis a subclass ofMyBaseClass:issubclass(MySubSubClass, MyBaseClass) # returns True issubclass(MySubClass, MyBaseClass) # returns True issubclass(MyBaseClass, MySubClass) # returns Falseissubclass(
type(obj), cls) vs issubclass(obj, cls):issubclass(type(obj), cls):type(obj)retrieves the class of the objectobj.issubclass()checks if the class ofobj(retrieved usingtype(obj)) is a subclass ofcls.This approach is useful when you want to check the class hierarchy relationship between two classes, one being the class of an object (
obj) and the other being a specific class (cls).
issubclass(obj, cls):objis the object whose class hierarchy relationship you want to examine.issubclass()checks ifobjis an instance ofclsor any of its subclasses.This approach is useful when you want to check if an object (
obj) belongs to a specific class (cls) or any of its subclasses.issubclass(type(obj), cls)checks if the class of an object is a subclass of a specific class, whileissubclass(obj, cls)checks if an object is an instance of a specific class or one of its subclasses
Note for testing :
Import Class Name in test.txt file
example: >>> MyClass = __import__('module_name').MyClass
Run doctest with -v : to get full details
python3 -m doctest -v ./tests/file.txt
See more ?
https://shazaali.substack.com/s/python








ونش انقاذ بالمعنى الحرفي ...استمري حفظك ووفقك ورعاكي الله
جزاكي الله كل الخير والله بجد