Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Inheritance

I want to create a class that is a type of graph, together with some additional information. As a simple example, let's say we want to create a class MyCayleyGraph that, when initialized on a group and a set of generators, returns the Cayley DiGraph, but also has attributes storing the group and the generators used to make that Cayley graph. I've found a way of doing this, but I fear it's not the best, and I'd like to know if there is a preferred way.

My attempt:

class MyCayleyGraph():
     def __init__(self, group, gens):
        self._graph = group.cayley_graph(generators = gens)
        self._group = group
        self._gens = gens
    def __getattr__(self, attr):
        return getattr(self._graph, attr)
    def group(self):
        return self._group
    def gens(self):
        return self._gens

What I'm concerned about:

Ideally, I think this should be done by inheriting from the graph class, but I couldn't figure out how to make MyCayleyGraph inherit from DiGraph, and make the initialized graph actually be the Cayley graph of group and gens, rather than just an empty graph. What bothers me about the solution above is that I seem to be "sneakily" making a DiGraph object, because I can call DiGraph attributes on the resulting Cayley Graph, but the resulting object isn't actually of type DiGraph.

What's the correct way to do this?

Inheritance

I want to create a class that is a type of graph, together with some additional information. As a simple example, let's say we want to create a class MyCayleyGraph that, when initialized on a group and a set of generators, returns the Cayley DiGraph, but also has attributes storing the group and the generators used to make that Cayley graph. I've found a way of doing this, but I fear it's not the best, and I'd like to know if there is a preferred way.

My attempt:

class MyCayleyGraph():
     def __init__(self, group, gens):
        self._graph = group.cayley_graph(generators = gens)
        self._group = group
        self._gens = gens
    def __getattr__(self, attr):
        return getattr(self._graph, attr)
    def group(self):
        return self._group
    def gens(self):
        return self._gens

What I'm concerned about:

Ideally, I think this should be done by inheriting from the graph class, but I couldn't figure out how to make MyCayleyGraph inherit from DiGraph, and make the initialized graph actually be the Cayley graph of group and gens, rather than just an empty graph. What bothers me about the solution above is that I seem to be "sneakily" making a DiGraph object, because I can call DiGraph attributes on the resulting Cayley Graph, but the resulting object isn't actually of type DiGraph.

What's the correct way to do this?