What is more Pythonic: subclass or adding functionality to base class?

Victor Porton porton at narod.ru
Sun Feb 11 07:30:24 EST 2018


The following is a real code fragment:

##########################################################
class PredicateParser(object, metaclass=ABCMeta):
    """
    Parses a given predicate (which may participate in several 
relationships)
    of a given RDF node.
    """

    def __init__(self, predicate):
        self.predicate = predicate

    @abstractmethod
    def parse(self, parse_context, graph, node):
        pass
##########################################################

Now I need to add new field on_error (taking one of three enumerated values) 
to some objects of this class.

What is more pythonic?

1. Create its subclass PredicateParserWithError and add the additional field 
on_error to this class.

2. Add on_error field to the base class, setting it to None by default, if 
the class's user does not need this field.

-- 
Victor Porton - http://portonvictor.org



More information about the Python-list mailing list