d3pd
d3pd

Reputation: 8315

how to create a network of instances of a class in Python

I have a simple class an instance of which can accept an input value and provide an output value. I want to create a network of instances of this class, with outputs of some instances linked to inputs of other instances. Is there a class or some other way of defining such a network in a clear, efficient way?

Upvotes: 1

Views: 340

Answers (1)

Raydel Miranda
Raydel Miranda

Reputation: 14360

What you need is to implement a directed graph. There are many ways to implement a graph I recommend you to read Python Patterns - Implementing Graphs.

A simple idea: You could hold a list of every instance you want to pass input to (that's also a graph).

class Node:

    def __init__(self):
        self.targets = []

    def output(self):
        for target in self.targets:
            target.process_input(self.produce_output())


    def process_input(self, input):
        # Process some data here.
        pass


    def produce_output():
        # Produce some data here.
        pass

Upvotes: 1

Related Questions