How to create contracts in python

Posted by recluze on Programmers See other posts from Programmers or by recluze
Published on 2012-12-09T15:42:12Z Indexed on 2012/12/09 17:16 UTC
Read the original article Hit count: 407

Filed under:
|
|
|

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.

© Programmers or respective owner

Related posts about java

Related posts about python