laitimes

Python Object-Oriented Programming OOP: Inheritance and Composition

Inheritance and composition

Inheritance and composition are two of the main concepts in object-oriented programming that model the relationship between two classes. They drive the design of the application.

They both support code reuse, but they are implemented in different ways

What is Inheritance?

The inheritance model, the so-called relationship. This means that when you have a class that inherits from a class, you create a relationship.

Python Object-Oriented Programming OOP: Inheritance and Composition

Base is the parent class, and Derived is the child class or derivative class

This is how the original inheritance relationship between classes is usually represented in Java

Derived  extends   Base           

Here's how it works in Python

Derived( Base)           

combination

Composition enables code to be reused by adding objects to other objects

Python Object-Oriented Programming OOP: Inheritance and Composition

Overview of inheritance in Python

Everything in Python is an object. Modules are objects, class definitions and functions are objects, and of course, objects created from classes are also objects.

Inheritance is a required feature of every object-oriented programming language. This means that Python supports inheritance.

Object Superclass (Parent)

>>> class MyClass:
...     pass
...           

This class doesn't seem to have any, but it's actually taken for granted

>>> c = MyClass()
>>> dir(c)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__']           

A normal object has some built-in methods even if it doesn't have arbitrary properties and methods added to it

>>> o = object()
>>> dir(o)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__']           

Everything is an object

class  MyClass:
    pass

c = MyClass()
print(c)

obj = object()
print(obj)
print(dir(obj))
print(isinstance(  c,object))           
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
<__main__.MyClass object at 0x00000246241E24F0>
<object object at 0x0000024623BA4D50>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
True           

Virtually all object numbers, string functions, are objects

def sya():
    pass
print(isinstance(  sya,object))
           
Python Object-Oriented Programming OOP: Inheritance and Composition
hello = "hello"
print(isinstance(hello, object))           
Python Object-Oriented Programming OOP: Inheritance and Composition

To be continued

You can see that all objects are an instance of an object, including numbers, strings, functions

Python Object-Oriented Programming OOP: Inheritance and Composition

Read on