Home Meta vs Abstract vs Parent Classes
Post
Cancel

Meta vs Abstract vs Parent Classes

In python everything is a object. Considering this

MetaClass is a class whose instances are classes.

We can not create any instance using abstract class.

Parent class is a classs we just use this have import all attibutes and functionalities.

Did not get it why late, let us dive in

MetaClass

Considering my above statement let us see class of each object. Try this in python3

1
2
3
4
5
class A():
    pass

A.__class__
<class 'type'>

So here class of a class is a type. Here type is a metaclass for all the classes in python. Now, we are going to create a meta class using this type as parent class.

1
2
3
4
5
class MetaA(type):
    def __new__(cls, name, bases, attrs):
        x=super().__new__(cls, name, bases, attrs)
        x.default_value = 100
        return x

The above class is a metaclass whose instance will be a class. Using this metaclass let us create a class and see its argument

1
2
3
4
5
class A(metaclass=MetaA):
    pass

A.default_value
100

Try following for better understanding the above metaclasses

1
2
3
4
5
6
7
8
9
10
11
12
13
a=type()
a=MetaA() # This throws an error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __new__() missing 3 required positional arguments: 'name', 'bases', and 'attrs'

B = type('B', (), {})
print(B)
<class '__main__.B'>

MetaB = MetaA('MetaB',(),{})
print (MetaB.default_value)

So here we can not directly creata a class from the metaclasses we use above shown format and the resultant will be a class.

Metaclass is the class of classes whose defined variables and methods will be default class variables and class methods for all the classes derived from the metaclass

AbstractClass

Abstract class is a blueprint for other classes. No instance can be created using the abstract class. But we can pass as parent class whose child class should have all the abstract methods defined in the abstract class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from abc import ABCMeta, abstractmethod

class BaseAbstract(metaclass=ABCMeta):

    @abstractmethod
    def testmethod(self):
        print("Abstract Method")

a=BaseAbstract()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Cant instantiate abstract class BaseAbstract with abstract methods test

class Base(BaseAbstract):
    pass

a=Base()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cant instantiate abstract class Base with abstract methods test

class Base(BaseAbstract):
     
     def test(self):
        print ("Hey Child")
 
b = Base()
b.test()
Hey Child

Parent Class

This class attributes and methods will be passed to all child classes we need to define them again but we can modify them.

  • We can pass more than one parent class
  • If two parent classes have same method preference comes from left.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A():

    def test(self):
        print("this is A class")


class B():

    def test(self):
        print ("This is B Class")

class C(A,B):
    pass

c= C()
c.test()
this is A class
This post is licensed under CC BY 4.0 by the author.

-

Open SSL Encryption

Comments powered by Disqus.