I do not want to let the A class be instantiated because, well, it is an abstract class. The problem is, this is actually allowed
Python on its own doesn't provide abstract classes. 'abc' module which provides the infrastructure for defining Abstract Base Classes.
Abstract classes are classes that contain one or more abstract methods. In your case code still an abstract class that should provide "Abstract classes cannot be instantiated" behavior.
If you don't want to allow, program need corrections:
i.e add decorator @abstractmethod.
Below code executed in python 3.6
from abc import ABC, abstractmethod
class Myabs(ABC):
def __init__(self, connection):
self.connection = connection
print("1. calling from base init : {}".format(self.connection))
super(Myabs, self).__init__()
@abstractmethod
def fun1(self, val):
pass
class D(Myabs):
def fun1(self, val="hi"):
print("calling from D: fun1")
object0 = Myabs('connection') # Python 3.6.9
output:
ClassConcept/abs_class.py Traceback (most recent call last): File
"ClassConcept/abs_class.py", line 19, in
object0 = Myabs('connection') # Python 3.6.9 TypeError: Can't instantiate abstract class Myabs with abstract methods fun1
Process finished with exit code 1