user3764112
user3764112

Reputation: 13

Call a method in an instance that a calling class implements - but is otherwise unrelated

Say I have this file structure:

main.py contains a class called Engine, and a method inside that, called midi_out

base_widg.py contains a class called BaseWidg

gui.py is the file that I run when I want to test the entire program, and implements both an instance of the Engine class in main.py and an instance of the BaseWidg class in base_widg.py

There is a method inside BaseWidg that needs to call the midi_out function in the implemented instance of Engine

EDIT

in the init function of Engine:

self.midiout = rtmidi.MidiOut()

This is important because many of the other methods need this declaration, including midi_out, because:

def midi_out(self, note, vel, state):
    # figure out what channel is (irrelevant)
    self.midiout.send_message([channel, note, vel)

Unfortunately this tends to suggest to me I cannot use @staticmethod...

What is the most pythonic and efficient way to do this? Do I need to change the structure of my program?

Potentially irrelevant: I am using python 2.7, pygtk 2 and python_rtmidi.

Upvotes: 1

Views: 58

Answers (1)

Addison
Addison

Reputation: 1075

One option is to have the constructor of BaseWidg take an instance of Engine and store it as an instance variable. Then you can just call the midi_out() method of that instance of Engine from within BaseWidg.

Upvotes: 1

Related Questions