2
from abc import ABC, abstractmethod

class Example(ABC):
    def smile(self):
        print("smile")

My understanding is that Example becomes an AbstractClass when it inherits the ABC class. An AbstractClass can't be instantiated but the following code executes without errors

example = Example()
Kun.tito
  • 165
  • 1
  • 7
  • Add the @abstractmethod decorator so that the class must be overridden before it is instantiated. – hypadr1v3 May 12 '21 at 07:49
  • Are you saying `Example` isn't an `AbstractClass` until it contains an `@abstractmethod` `decorator`? what then does inheriting `ABC` do? – Kun.tito May 12 '21 at 18:07
  • It does become abstract, although the methods that are contained inside are concrete. You need to make the methods inside abstract for you to not be able to instantiate it. – hypadr1v3 May 14 '21 at 08:02

1 Answers1

1

Your class does become abstract, although the method/methods that are contained inside (which is smile) is/are concrete. You need to make the methods inside abstract using the decorator @abc.abstractclass. Thus, if you rewrite your code to:

from abc import ABC, abstractmethod

class Example(ABC):
    @abstractmethod
    def smile(self):
        print("smile")
        
e = Example()

then you would get the error:

TypeError: Can't instantiate abstract class Example with abstract methods smile
hypadr1v3
  • 543
  • 4
  • 26