How to create contracts in python
- by recluze
I am just moving to python from Java and have a question about the way the two do things. My question relates to contracts. An example: an application defines an interface that all plugins must implement and then the main application can call it. In Java:
public interface IPlugin {
public Image modify(Image img);
}
public class MainApp {
public main_app_logic() {
String pluginName = "com.example.myplugin";
IPlugin x = (IPlugin) Class.forName(pluginName);
x.modify(someimg);
}
}
The plugin implements the interface and we use reflection in main app to call it. That way, there's a contract between the main app and the plugin that both can refer to.
How does one go about doing something similar in Python? And also, which approach is better?
p.s. I'm not posting this on SO because I'm much more concerned with the philosophy behind the two approaches.