[Tutor] intefaces in python
amit.pureenergy at gmail.com
Mon Jun 29 14:04:21 CEST 2009
I think ideally i want a compile Error just like java .. but from the
discussion here ... i wrote this little example:
At least by this when ever the derived class object is created without
implementing a particular function ,it raises a NotImplementedError
which i can later use.
You have said
"you could arrange that when the plugin is first encountered, you
validate that it has all the required methods and data members. Not
by calling them, but by scanning the object for their existence."
that would be ideal ... can you enlighten me on this how may one do that.
On Mon, Jun 29, 2009 at 5:01 PM, Dave Angel <davea at ieee.org> wrote:
> Amit Sethi wrote:
>> Well I want to implement plug-in like mechanism for an application . I want
>> to define some minimum functions that any body writing a plugin has to
>> implement. For that i thought an interface would be best because in a
>> scenario where the function is not implemented some kind of error would
>> occur. I would love to hear if you think their is a better way to achieve
> In Java, deriving a class from such an interface, but neglecting to implement those methods will cause a compile error (if I recall correctly, it's been several years). In Python, the error will happen at run time. But an existing runtime error will occur even without such an interface, so it wouldn't seem you gain much. In Python, the interface does two things:
> 1) it's a comment, a common place to look for certain behavior.
> 2) it's potentially a source for an IDE to provide tool-tips or code completion
> 3) it can generate a different error, which is perhaps more useful to the developer unsure of how the method is spelled or used. This way he/she knows whether to fix the caller or the implementation.
> #3 seems valid to me.
> However, for the particular use-case, you might want to stretch a bit further. Since you've got one "user" (your code), and many "providers" (the plug-in writers) perhaps you could arrange that when the plugin is first encountered, you validate that it has all the required methods and data members. Not by calling them, but by scanning the object for their existence.
> Tutor maillist - Tutor at python.org
More information about the Tutor