Using a base class but defining a property I have designed a Python TreeNodeBase class, so all tree nodes I implement can inherit from this. However, due to the laziness inherent in a lot of trees, I would like some fields to be properties (using the @property decorator) instead.

But doing so, the base class will raise AttributeError whenever it tries to set a field, when I call super().__init__(). The way I'm resolving this now is to ignore the AttributeError. Is this a good idea, and is there a standard approach to this problem?

from contextlib import suppress

class TreeNodeBase:
    def __init__(self, value, edge_value=None, children=None, parent=None, tree=None):
        with suppress(AttributeError):
            self.value = value
        with suppress(AttributeError):
            self.edge_value = edge_value
        with suppress(AttributeError):
            self.children = children
        with suppress(AttributeError):
            self.parent = parent
        self.tree = tree

    def get_child(self, edge_value):
        raise NotImplementedError
You solution does look a bit fragile, but I can't help that without seeing more of the hierarchy. The way I'd approach this is to add value=None to your argument function header, and then do this for each attribute:

if value is not None:
    self.value = value


That way the inheriting classes can signal that they don't want the attribute initialised by just leaving the attribute out of the __init__ args, or passing None (this won't work if you actually want None as a value. You'd have to find another placeholder). That way, the system will be a bit more flexible, and there will not be any bad consequences caused by trying to initialise an attribute which is not meant to be initialised.