[Tutor] intefaces in python

Amit Sethi amit.pureenergy at gmail.com
Mon Jun 29 14:19:54 CEST 2009


wait even in the above example i would have to run all the functions
in __init__ that is plain stupid ... i was just brain storming....

On Mon, Jun 29, 2009 at 5:34 PM, Amit Sethi<amit.pureenergy at gmail.com> wrote:
> I think ideally i want a compile Error just like java .. but from the
> discussion here ... i wrote this little example:
>
> class a(object):
>     def __init__(self):
>         self.query()
>         try:
>             if self.query_not_implemented==True:
>                 raise NotImplementedError
>         except AttributeError:
>             pass
>     def query(self):
>               self.query_not_implemented=True
>
> 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.
>
>
> @Dave Angel
>
> 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
>>> this
>>
>> 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
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> A-M-I-T S|S
>



-- 
A-M-I-T S|S


More information about the Tutor mailing list