Two variants of class hierachy

Victor Porton porton at narod.ru
Sat Nov 19 10:00:09 EST 2016


I am developing software which shows hierarchical information (tree), 
including issues and comments from BitBucket (comments are sub-nodes of 
issues, thus it forms a tree).

There are two kinds of objects in the hierarchy: a. with a (possibly long) 
paginated list of childs; b. with a short list of strings, each string being 
associated with a child object.

I have two variants of class inheritance in mind. Please help to decide 
which is better.	

The first one declares only one base class, but some its method remain 
unimplemented (raise NotImplementedError) even in derived classes.

The second one defines two distinct base classes 
HierarchyLevelWithPagination (for objects of above described class "a") and 
HierarchyLevelWithShortList (for objects of above described class "b"), but 
use multiple inheritance.

# VARIANT 1: #

class HierarchyLevel(object):
    def get_node(self):
        return None

    def childs(self, url, page, per_page=None):
        raise NotImplementedError()

    def short_childs(self):
        raise NotImplementedError()

class BitBucketHierarchyLevel(HierarchyLevel):
    ...

# A implements only childs() but not short_childs()
class A(BitBucketHierarchyLevel):
    ...

# B implements only short_childs() but not childs()
class B(BitBucketHierarchyLevel):
    ...

## OR ##
# VARIANT 2: #

class HierarchyLevel(object):
    def get_node(self):
        return None

class HierarchyLevelWithPagination(HierarchyLevel):
    def childs(self, url, page, per_page=None):
        raise NotImplementedError()

class HierarchyLevelWithShortList(HierarchyLevel):
    def short_childs(self):
        raise NotImplementedError()

## THEN ##

# code specific for BitBucket
class BitBucketHierarchyLevel(HierarchyLevel):
    ...

# diamonds:

class A(BitBucketHierarchyLevel, HierarchyLevelWithPagination):
    ...

class B(BitBucketHierarchyLevel, HierarchyLevelWithShortList):
    ...

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



More information about the Python-list mailing list