Before describing my solution, let me introduce you how Python class instances are created:

Figure 1: Python Instance creation [1]
Given the above description, you can see that in Python class instances are actually created by a Metaclass. As we can see, when the caller is creating an instance of our class, first the __call__ magic method is called which in turn is calling the __new__ and __init__ of the class and then __cal__ is returning the object instance back to the caller.
With all that said, we can simply try to do our checking if the instance created by __init__ actually defines those "required" attributes.
Metaclass
class ForceRequiredAttributeDefinitionMeta(type):
def __call__(cls, *args, **kwargs):
class_object = type.__call__(cls, *args, **kwargs)
class_object.check_required_attributes()
return class_object
As you can see in __call__ what we do is to create the class object, and then call its check_required_attributes() method which will check if the required attributes have been defined. In there if the required attributes are not defined we should simply throw an error.
Superclass
Python 2
class ForceRequiredAttributeDefinition(object):
__metaclass__ = ForceRequiredAttributeDefinitionMeta
starting_day_of_week = None
def check_required_attributes(self):
if self.starting_day_of_week is None:
raise NotImplementedError('Subclass must define self.starting_day_of_week attribute. \n This attribute should define the first day of the week.')
Python 3
class ForceRequiredAttributeDefinition(metaclass=ForceRequiredAttributeDefinitionMeta):
starting_day_of_week = None
def check_required_attributes(self):
if self.starting_day_of_week is None:
raise NotImplementedError('Subclass must define self.starting_day_of_week attribute. \n This attribute should define the first day of the week.')
Here we define the actual superclass. Three things:
- Should make use of our metaclass.
- Should define the required attributes as
None see starting_day_of_week = None
- Should implement the
check_required_attributes method that checks if the required attributes are None and if they are to throw a NotImplementedError with a reasonable error message to the user.
Example of a working and non-working subclass
class ConcereteValidExample(ForceRequiredAttributeDefinition):
def __init__(self):
self.starting_day_of_week = "Monday"
class ConcereteInvalidExample(ForceRequiredAttributeDefinition):
def __init__(self):
# This will throw an error because self.starting_day_of_week is not defined.
pass
Output
Traceback (most recent call last):
File "test.py", line 50, in <module>
ConcereteInvalidExample() # This will throw an NotImplementedError straightaway
File "test.py", line 18, in __call__
obj.check_required_attributes()
File "test.py", line 36, in check_required_attributes
raise NotImplementedError('Subclass must define self.starting_day_of_week attribute. \n This attribute should define the first day of the week.')
NotImplementedError: Subclass must define self.starting_day_of_week attribute.
This attribute should define the first day of the week.
As you can see, the first instance created successfully since was defining the required attribute, where the second one raised a NotImplementedError straightaway.
def process_pages(list_of_pages): return [ page.page_name for page in list_of_pages ]- chepner