30 How to Create an Abstract Class in Python programming?

Publicado em: 13 Dezembro 2023
no canal de: Free Tutorial TV
3
1

One is a shape class another is a square class in shape class.

I have two empty method.

One is area and other is perimeter.

And I'm using shape as a super class and square as a subclass and square class is inheriting from the

Shape class.

Now in this example I want to do two things.

One is I don't want to allow the other users to create an instance of a Shape class because Shape class

acts like a template for the square classs so I don't want to allow those users to create an instance of

this Shape class.

So let's see right now if we can create an instance of the Shape class or not so I'm going to do just

the right shape is equal to shape.

And when I run that code my code runs fine.

That means right now I'm able to create an instance of the Shape class which I don't want.

The second thing is I want to make sure that both these method which are there inside the shape class

are implemented inside the square class.

So I want to make sure that whoever is using the Shape class he or she must implement these two method

inside the subclass.

So how can I achieve these two things.

Now it turns out that abstract classes are just made for that.

So how to create an abstract class in Python.

The fact is python on it's own doesn't provide any abstract class but don't worry there is a built in

module in python which we can use to create an abstract class.

So let's import this built in model first so I'm going to just write from a b c import capital A B C

comma abstract method.

So these are the two things we need to import from this module.

Now this module ABC stands for abstract base classes. and this allows us to create the abstract classes.

So how we can create an abstract class using this ABC module.

So what we need to do here is we need to inherit from the ABC module into the shape class.

OK.

So shape class is inheriting from the ABC module. and once this shape class inherit from the ABC module.

The second thing what we need to do here is we need to add a decorator here. and how to add a decorator.

We just need to use at the rate.

And then the name of this abstract method which is like this.

And let me use this abstract method decorator.

On top of this parameter method also what this abstract method.

Decorator does is it makes these two methods abstract.

And what is an abstract method and abstract method is a method which you must implement in the subclass.

So when you run the code now it will give us this error.

It says cant instantiate abstract class shape with the abstract method.

**************************************************************************
please like and follow us in Facebook and Instagram.
facebook:   / technologytv  


Instagram:   / heikhamsingh  


Twitter:   / hanandkumarsing  


Nesta página do site você pode assistir ao vídeo on-line 30 How to Create an Abstract Class in Python programming? duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Free Tutorial TV 13 Dezembro 2023, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 3 vezes e gostou 1 espectadores. Boa visualização!