Python Forum
Inheritance vs Instantiation for Python classes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inheritance vs Instantiation for Python classes
#1
I know the meaning of Instantiation and Inheritance.
I can inherit a class and use it methods and attributes.
I also can instantiate a class and still can use the same methods and attributes !!

I am just wondering why there exists two solutions for the same problem.
is there any advantage of using one over the other (memory, speed, ..)

Inheritance:
class A:        # define your class A
pass
class B:         # define your class B
pass
class C(A, B):   # subclass of A and B
Instantiation:
class Foo():
        def __init__(self,x,y):
            print (x+y)
f = Foo(3,4)
Reply
#2
(Sep-23-2021, 12:31 PM)mr_byte31 Wrote: I am just wondering why there exists two solutions for the same problem.
well, these are two completely different concepts.

Class is a blue-print. Very rough comparison between the two:

Instantiation is creating multiple items (i.e. instances) based on the same blue print. All instances of the same class have exactly the same attributes.

Inheritance is when one class have some attributes from the parent class (or classes in case of multiple inheritance), but at the same time extend the parent class with new attributes or overload some attributes with different implementation.

e.g. having class Animal and then class Mammal, class Bird and class Reptile, etc. that inherit from it. You will instantiate child class if you need an object that is instance of e.g. class Mammal.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I understand what you said

some one can instantiate an object from a class and start to use its method to extend the functionality of other class.
still inheritance was not needed !
Reply
#4
(Sep-23-2021, 04:21 PM)mr_byte31 Wrote: I understand what you said

some one can instantiate an object from a class and start to use its method to extend the functionality of other class.
still inheritance was not needed !


No, it sounds like you still don't understand.

I found the site from where you get these examples and they do really poor job at explaining the difference between the two, while using word "inherit" in both cases, which adds to the confusion.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I didn't invent the examples. I just copied them
I provided them to show the way they get written.

someone can instantiate an object and get all functionality from methods and add his functionality and top and this still work !!!
Reply
#6
(Sep-23-2021, 04:21 PM)mr_byte31 Wrote: some one can instantiate an object from a class and start to use its method to extend the functionality of other class.
I think it would help if you were to provide something authoritive that says what you are saying. I do not know what extend the functionality of other class means. I consider Classes — Python 3.9.7 documentation to be authoritive enough. Where does it say what you mean to say? Hopefully that will provide us with a clear understanding of what you are saying.
Reply
#7
Here is an example of inheritance

class Shape:
    """Basic shape class

    """
    def __init__(self, n):
        self.n = n
        print('this is __init__ of class Shape') # this is just to show the call of __init__

    def __str__(self):
        return f"Shape with {self.n} sides"


class Rectangle(Shape):
    """This class represents rectangle.

    Inherits from Shape, but adds functionality to calculate perimeter
    """

    def __init__(self, height, width):
        # call __init__ of the parent class Shape
        super().__init__(4)
        self.height = height
        self.width = width
        print('this is __init__ of class Rectangle') # this is just to show the call of __init__

    @property
    def perimeter(self):
        return 2 * (self.height + self.width)


    def __str__(self):
        return f"Rectangle {self.height} x {self.width}"



class Square(Rectangle):
    """This class represents square.

    Inherits from Rectangle.
    """
   
    def __init__(self, side):
        super().__init__(side, side)
        self.side = side
        print('This is __init__ of class Square') # this is just to show the call of __init__


    def __str__(self):
        return f"Square {self.height} x {self.width}"
Here we have 3 classes - class Shape - very basic class, class Rectangle, that inherits from Shape and add extra functionality to return perimeter and overwrite the magic method __str__ of the parent class, and class Square, that inherits from Rectangle, but overwrites the __str__ method. So far this is inheritance, no instantiation whatsoever, NONE.

Let's add some more code, example of instantiation

# Examples of instantiation

# Create 2 instances of class Shape

shape = Shape(3)
shape2 = Shape(5)

print(shape)
print(shape2)

print('\n------------------\n')

# Create instance of Rectangle
rectangle = Rectangle(3, 5)
print(rectangle)
print(f'Sides: {rectangle.n}') # use attribute n, inherited from Shape
print(f'Height: {rectangle.height}')
print(f'Width: {rectangle.width}')
print(f'Perimeter: {rectangle.perimeter}')

print('\n------------------\n')

# Create instance of Square
square = Square(10)
print(square)
print(f'Sides: {square.n}') # use attribute n, inherited from Rectangle, inherited from Shape
print(f'Height: {square.height}') # use attribute height, inherited from Rectangle
print(f'Width: {square.width}') # use attribute width, inherited from Rectangle
print(f'Perimeter: {square.perimeter}') # use attribute perimeter, inherited from Rectangle
This is example of instantiation. We create 2 instances of class Shape and 1 Rectangle and 1 Square.

Now, if you run the code, the output is as follows

Output:
this is __init__ of class Shape this is __init__ of class Shape Shape with 3 sides Shape with 5 sides ------------------ this is __init__ of class Shape this is __init__ of class Rectangle Rectangle 3 x 5 Sides: 4 Height: 3 Width: 5 Perimeter: 16 ------------------ this is __init__ of class Shape this is __init__ of class Rectangle This is __init__ of class Square Square 10 x 10 Sides: 4 Height: 10 Width: 10 Perimeter: 40
mr_byte31 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
you explained it very well to me.
thanks.

I am coming from C language background and it is sometimes not easy to accept OOP concepts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Classes rob101 4 468 Feb-05-2024, 06:51 PM
Last Post: rob101
  Understanding Python classes PythonNewbee 3 1,150 Nov-10-2022, 11:07 PM
Last Post: deanhystad
Sad Python classes PythonNewbee 4 995 Nov-09-2022, 01:19 PM
Last Post: deanhystad
  Understanding Python super() for classes OmegaRed94 1 1,795 Jun-09-2021, 09:02 AM
Last Post: buran
  Python classes Python_User 15 4,756 Aug-04-2020, 06:57 PM
Last Post: Python_User
  XlsxWriter and Python inheritance aquerci 1 2,465 May-05-2020, 09:36 AM
Last Post: buran
  Python Classes leodavinci1990 1 2,056 Nov-27-2019, 07:25 AM
Last Post: buran
  How inheritance works in Python ARV 1 1,803 Oct-03-2019, 03:06 PM
Last Post: Larz60+
  Problems with inheritance with classes internetguy 3 2,547 Jul-04-2019, 11:59 AM
Last Post: metulburr
  Unexpected Output using classes and inheritance langley 2 1,914 Jul-04-2019, 09:33 AM
Last Post: langley

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020