Setting the ABCMeta metaclass

suggest change

Abstract classes are classes that are meant to be inherited but avoid implementing specific methods, leaving behind only method signatures that subclasses must implement.

Abstract classes are useful for defining and enforcing class abstractions at a high level, similar to the concept of interfaces in typed languages, without the need for method implementation.

One conceptual approach to defining an abstract class is to stub out the class methods, and then raise a NotImplementedError if accessed. This prevents children classes from accessing parent methods without overriding them first. Like so:

class Fruit:
    
    def check_ripeness(self):
        raise NotImplementedError("check_ripeness method not implemented!")
class Apple(Fruit):
    pass

a = Apple()
a.check_ripeness() # raises NotImplementedError

Creating an abstract class in this way prevents improper usage of methods that are not overriden, and certainly encourages methods to be defined in child classes, but it does not enforce their definition. With the abc module we can prevent child classes from being instantiated when they fail to override abstract class methods of their parents and ancestors:

from abc import ABCMeta

class AbstractClass(object):
    # the metaclass attribute must always be set as a class variable 
    __metaclass__ = ABCMeta

   # the abstractmethod decorator registers this method as undefined
   @abstractmethod 
   def virtual_method_subclasses_must_define(self):
       # Can be left completely blank, or a base implementation can be provided
       # Note that ordinarily a blank interpretation implicitly returns `None`, 
       # but by registering, this behaviour is no longer enforced.

It is now possible to simply subclass and override:

class Subclass(AbstractClass):
    def virtual_method_subclasses_must_define(self):
        return

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Abstract Base Classes:
* Setting the ABCMeta metaclass

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
134 Abstract Base Classes
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve