Re: [Tutor] intefaces in python

2009-06-30 Thread Jan Ulrich Hasecke


Am 28.06.2009 um 17:00 schrieb Amit Sethi:

Hi , I don't suppose python has a concept of interfaces. But can  
somebody tell me if their is a way i can  implement something like a  
java interface in python.


The Web Framework Zope has a notion of interfaces.
http://pypi.python.org/pypi/zope.interface/3.5.1

There are some quite interesting things you can make with interfaces.  
Eg. the Component Architecture is build on interfaces and adapters.


http://wiki.zope.org/zope3/WhatAreInterfaces

More on this here:
http://docs.zope.org/

juh

smime.p7s
Description: S/MIME cryptographic signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Alan Gauld


"Amit Sethi"  wrote 

I think ideally i want a compile Error just like java .. 


I think you are trying to make Python act like Java which 
is always a really bad mistake when using a programming 
language. As Bjarne Stroustrup used to say (repeatedly) 
"C++ is not Smalltalk". And Python is not Java.


If you stop trying to design like Java and start using 
the extra power of Python you will find your solutions 
are both more flexible and more simple.


Python does not compile so trying to build in "compile time" 
checking makes no sense. You could do some checks 
immediately after class definition or more likely class 
assignment but how would it really benefit you?


Get used to how interpreted dynamic languages work 
and use their features to your benefit instead of trying 
to force them to act like statically typed compiled 
languages. It will give you a great sense of freedom!



--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Amit Sethi
well dir(object) , how would that help . All the functions in base
class would automatically be inherited by the objects of plug-in class
... so they would come in the list even if it was not implemented...

On Mon, Jun 29, 2009 at 5:58 PM, Luke
Paireepinart wrote:
> Amit Sethi wrote:
>>
>> I think ideally i want a compile Error just like java ..
>
> Why?  For developers, so they'll know if their plugin meets the interface
> requirements?
> Have you considered just making a unit test that will call all interface
> functions with appropriate parameters?  Then they can just run the test on
> their code and if it passes they will know they implemented the minimum
> required functions (whether they do what they're supposed to do is a
> different story...)  It would provide the same level of interface security
> as Java would, except it would allow them to decide how much of the contract
> they need to enforce for their specific application. That's one of the
> things I like about python... for example, if I want to override stdout so
> all my print statements go to a file, I just have to create a new class that
> has a write() method that writes anything it's passed to a file... I don't
> have to implement all of the other functionality that whatever object
> resides there before has.  So in that case, if I ran your unit test on my
> replacement stdout, it may say "Hey, you failed the writeline() test, your
> object doesn't have this!" but since I know I'm not going to use writeline,
> I can just ignore the warning.  Or if I meant for it to have writeline, I
> can say "oh snap I need to go implement writeline!"  But as I said, that
> wouldn't enforce the interface, just inform them of it if they desired that,
> which I would personally prefer but you may not.
> That's really the idea behind duck typing.  Assume that they wrote
> sufficient code to handle whatever you use it for and if they didn't it'll
> blow up in their face.
>>
>> @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.
>>
>>
>
> Not sure if it's what he meant, but you can just do a dir(object) and it
> will return a list with all method / member names.  then you could just
> confirm that the names are there.
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Luke Paireepinart

Amit Sethi wrote:

I think ideally i want a compile Error just like java ..
Why?  For developers, so they'll know if their plugin meets the 
interface requirements?
Have you considered just making a unit test that will call all interface 
functions with appropriate parameters?  Then they can just run the test 
on their code and if it passes they will know they implemented the 
minimum required functions (whether they do what they're supposed to do 
is a different story...)  It would provide the same level of interface 
security as Java would, except it would allow them to decide how much of 
the contract they need to enforce for their specific application. That's 
one of the things I like about python... for example, if I want to 
override stdout so all my print statements go to a file, I just have to 
create a new class that has a write() method that writes anything it's 
passed to a file... I don't have to implement all of the other 
functionality that whatever object resides there before has.  So in that 
case, if I ran your unit test on my replacement stdout, it may say "Hey, 
you failed the writeline() test, your object doesn't have this!" but 
since I know I'm not going to use writeline, I can just ignore the 
warning.  Or if I meant for it to have writeline, I can say "oh snap I 
need to go implement writeline!"  But as I said, that wouldn't enforce 
the interface, just inform them of it if they desired that, which I 
would personally prefer but you may not.
That's really the idea behind duck typing.  Assume that they wrote 
sufficient code to handle whatever you use it for and if they didn't 
it'll blow up in their face.

@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.

  
Not sure if it's what he meant, but you can just do a dir(object) and it 
will return a list with all method / member names.  then you could just 
confirm that the names are there.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Amit Sethi
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 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  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  -  tu...@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> A-M-I-T S|S
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Amit Sethi
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  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  -  tu...@python.org
> http://mail.python.org/mailman/listinfo/tutor



--
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Dave Angel

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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Alan Gauld


"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


Well you could define a real default plugin class that actually does 
something.

Then anyone who wants to create a plugin can inherit from that and either
extend the default functions or override them with something different.

That will reduce the code that plug-in writers need to create and prevent
any error messages appearing for the user.

It also means the default plug in acts as sample code for the interface 
too.


HTH,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Amit Sethi
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

On Mon, Jun 29, 2009 at 2:49 PM, Alan Gauld wrote:

> "Amit Sethi"  wrote
>
>> class MyInterface(object):
>>  doSomething(line):
>>   raise NotImplementedError
>>  doSomethingElse(line):
>>  raise NotImplementedError
>>
>> I think that is exactly the kind of structure i was looking for ...
>>
>
> As a matter of interest, why? What do you anticipate using this for?
> I have found a few cases where abstract interfaces are useful but they are
> very few and far between.
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Alan Gauld
"Amit Sethi"  wrote 


class MyInterface(object):
  doSomething(line):
   raise NotImplementedError
  doSomethingElse(line):
  raise NotImplementedError

I think that is exactly the kind of structure i was looking for ...


As a matter of interest, why? 
What do you anticipate using this for?
I have found a few cases where abstract interfaces are useful 
but they are very few and far between.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Amit Sethi
class MyInterface(object):
   doSomething(line):
   raise NotImplementedError
   doSomethingElse(line):
   raise NotImplementedError


I think that is exactly the kind of structure i was looking for ...

On Mon, Jun 29, 2009 at 12:58 PM, Andre Engels wrote:

> On Sun, Jun 28, 2009 at 5:00 PM, Amit Sethi
> wrote:
> > Hi , I don't suppose python has a concept of interfaces. But can somebody
> > tell me if their is a way i can  implement something like a java
> interface
> > in python.
>
> Sure. Interfaces are just Java's compensation for not having multiple
> inheritance. Python does have multiple inheritance, so that's what one
> would use. Although one could also use duck typing, and then use
> 'nothing' as an implementation...
>
> More specific:
>
> 
> Java Interface:
> public interface MyInterface {
>string doSomething(string line);
>string doSomethingElse(string line);
> }
>
> Java Implementation:
> public class MyImplementation {
>   string doSomething(string line) {
>   return "I did something with" + line;
>   }
>   string doSomethingElse(string line) {
>  return "I did something else."
>   }
> }
>
> ==
> Python Interface:
>
> class MyInterface(object):
>doSomething(line):
>raise NotImplementedError
>doSomethingElse(line):
>raise NotImplementedError
>
> Python Implementation:
> class MyImplementation(MyInterface):
>doSomething(line):
>return "I did something with "+line
>doSomethingElse(line):
>return "I did something else."
>
> ==
> Python interface using duck typing:
>
> # Hey guys, when you call something a 'MyInterface', it needs methods
> doSomething and doSomethingElse
>
> Python Implementation using duck typing:
>
> class MyImplementation(object):
># These things implement MyInterface
>doSomething(line):
>return "I did something with "+line
>doSomethingElse(line):
>return "I did something else."
>
>
> --
> André Engels, andreeng...@gmail.com
>



-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-29 Thread Andre Engels
On Sun, Jun 28, 2009 at 5:00 PM, Amit Sethi wrote:
> Hi , I don't suppose python has a concept of interfaces. But can somebody
> tell me if their is a way i can  implement something like a java interface
> in python.

Sure. Interfaces are just Java's compensation for not having multiple
inheritance. Python does have multiple inheritance, so that's what one
would use. Although one could also use duck typing, and then use
'nothing' as an implementation...

More specific:


Java Interface:
public interface MyInterface {
string doSomething(string line);
string doSomethingElse(string line);
}

Java Implementation:
public class MyImplementation {
   string doSomething(string line) {
   return "I did something with" + line;
   }
   string doSomethingElse(string line) {
  return "I did something else."
   }
}

==
Python Interface:

class MyInterface(object):
doSomething(line):
raise NotImplementedError
doSomethingElse(line):
raise NotImplementedError

Python Implementation:
class MyImplementation(MyInterface):
doSomething(line):
return "I did something with "+line
doSomethingElse(line):
return "I did something else."

==
Python interface using duck typing:

# Hey guys, when you call something a 'MyInterface', it needs methods
doSomething and doSomethingElse

Python Implementation using duck typing:

class MyImplementation(object):
# These things implement MyInterface
doSomething(line):
return "I did something with "+line
doSomethingElse(line):
return "I did something else."


-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-28 Thread Lie Ryan
Bob Rea wrote:
> On Sun June 28 2009 1:48 pm, Alan Gauld wrote:
>> advantages to them over defining, say, a mixin
> 
> Noob, here, wanted to know what a mixin is
> eh, just getting back into learning python
> googled it, still not sure what it is, but
> wikipedia says the name come from ice cream mixins at 
> Steve's Ice Cream Parlor
> 
> Ah yes, i was a frequenter there
> 
> Will watch out for what it is when I get farther into python
> may have to name them things like jimmies and 
> heath_bar_crunch
> 

In object-oriented programming languages, a mixin is a class that
provides a certain functionality to be inherited by a subclass, while
not meant for instantiation (the generating of objects of that class).
Inheriting from a mixin is not a form of specialization but is rather a
means of collecting functionality. A class may inherit most or all of
its functionality from one or more mixins through multiple inheritance.
  -- http://en.wikipedia.org/wiki/Mixin

In regular inheritance, you inherit from another class to inherit its
interfaces. In mixin inheritance, you inherit for its implementations.
The mixin concept takes it a bit further; the mixin class (parent class)
may not be usable by itself (i.e. instantiating a mixin class may not
make any sense) and must be inherited .

Example:

# Flavours
class Vanilla(object): pass
class Chocolate(object): pass

# Additions
class Nuts(object): pass
class Cookies(object): pass

# Items
class IceCream(): pass

# Usable classes
class ChocolateIceCream(Chocolate, IceCream): pass
class VanillaAndNutsIceCream(Vanilla, Nuts, IceCream): pass
class ChocolateAndCookiesIceCream(Chocolate, Cookies, IceCream): pass

Vanilla(), Chocolate(), Nuts(), and Cookies() are not meant to be
instantiated directly; they are meant to be inherited. These classes are
called mixin classes.

In python's standard lib, an example of mixin class is threading.Thread()

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-28 Thread Bob Rea
On Sun June 28 2009 1:48 pm, Alan Gauld wrote:
> advantages to them over defining, say, a mixin

Noob, here, wanted to know what a mixin is
eh, just getting back into learning python
googled it, still not sure what it is, but
wikipedia says the name come from ice cream mixins at 
Steve's Ice Cream Parlor

Ah yes, i was a frequenter there

Will watch out for what it is when I get farther into python
may have to name them things like jimmies and 
heath_bar_crunch

-- 
Bob Rea
mailto:gapet...@stsams.org
http://www.petard.us
http://www.petard.us/blog
http://www.petard.us/gallery

Where is Bill Stringfellow
now that we really need him?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] intefaces in python

2009-06-28 Thread Alan Gauld
"Amit Sethi"  wrote 

Hi , I don't suppose python has a concept of interfaces. 


Yes and No, I'll come back to that...

But can somebody tell me if their is a way i can  implement 
something like a java interface in python.


First, can you tell me why you would want to?
Java style interfaces tend to make your code much less 
reusable and much less flexible if you are using a dynamically 
typed language. There are very few real advantages to them
over defining, say, a mixin or using multiple inheritance (which 
of course Java can't do)


The normal way of defining a Java style Python interface 
class is simply to define a class that has a set of methods
thaty either do or return nothing or raise some kind of 
NotImplementedError exception. But usually providing 
a mixin is a much more powerful style of programming 
in Python since you can provide partial implementations 
of the methods or generic methods that are not dependant 
on the types of the parameters.


Coming back to the original question, Python has a very 
strong concept of an interface, that is how it checks types, 
if an interface does not exist it will raise a TypeError. But it 
does it at runtime and it does it at the method level not the 
class level. Very different to Microsoft's concept which was 
designed to meet the needs of COM and was subsequently 
adopted by Java.


There has also been talk of introducing syntax to create interfaces 
into Python which I personally think is a very, very poor idea! 
But quite a lot of what I think are poor ideas get intro Python 
so that doesn't mean much! :-)


Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] intefaces in python

2009-06-28 Thread Amit Sethi
Hi , I don't suppose python has a concept of interfaces. But can somebody
tell me if their is a way i can  implement something like a java interface
in python.
-- 
A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor