Re: Behaviour-based interface/protocol implementation?

2011-01-28 Thread Daniel Urban
On Fri, Jan 28, 2011 at 11:32, Alan Franzoni wrote: > On Thu, Jan 27, 2011 at 11:41 PM, Daniel Urban wrote: > >> Actually it can. You don't have to modify the object, just check for >> the desired methods/signature/whatever. See for example the >> implementation of collections.Hashable.__subclass

Re: Behaviour-based interface/protocol implementation?

2011-01-28 Thread Alan Franzoni
On Thu, Jan 27, 2011 at 11:41 PM, Daniel Urban wrote: > Actually it can. You don't have to modify the object, just check for > the desired methods/signature/whatever. See for example the > implementation of collections.Hashable.__subclasshook__ in _abcoll.py > and the abc.ABCMeta.__instancecheck_

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Daniel Urban
On Thu, Jan 27, 2011 at 20:05, Alan Franzoni wrote: > On Thu, Jan 27, 2011 at 8:03 PM, Alan Franzoni wrote: >> Yes, __instancecheck__ could be used as an alternative hook with >> respect to maybe_implemented_by(), but there's no such logic for >> signature checking. That's a minor detail, I think

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Alan Franzoni
On Thu, Jan 27, 2011 at 10:30 PM, Carl Banks wrote: > Write some kind of signature proxy to do it. I don't have a specific implementation idea yet, I see how that grows. > Based on this thread, you have quite specific requirements, so it's > doubtful someone else has implemented exactly what you

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Carl Banks
On Jan 24, 2:13 am, Alan Franzoni wrote: > Hello, > I'd like to have a system which lets me do certain actions if the > duck-type of a certain objects matches what I expect, i.e. I'd like to > have a formalization of what it's sometimes done through getattr() > calls: > > if getattr(myobj, "someme

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Alan Franzoni
On Thu, Jan 27, 2011 at 8:58 PM, Ethan Furman wrote: > When you signature check, do you mean counting the number of arguments, or > actually checking argument types? In order to check for argument types I should either assume type annotation (python 3 only, optional) or parse the docstring for th

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Ethan Furman
Alan Franzoni wrote: Hello, I'd like to have a system which lets me do certain actions if the duck-type of a certain objects matches what I expect, i.e. I'd like to have a formalization of what it's sometimes done through getattr() calls: if getattr(myobj, "somemethod", None) is not None: my

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Alan Franzoni
On Thu, Jan 27, 2011 at 8:03 PM, Alan Franzoni wrote: > Yes, __instancecheck__ could be used as an alternative hook with > respect to maybe_implemented_by(), but there's no such logic for > signature checking. That's a minor detail, I think. On the contrary, now that I double checked, it can't be

Re: Behaviour-based interface/protocol implementation?

2011-01-27 Thread Alan Franzoni
On Wed, Jan 26, 2011 at 7:23 PM, Daniel Urban wrote: >> That's just what I'd like and I suppose can't be currently done with >> current ABC, PyProtocols or zope.interface implementations, right? > > It can. With __instancecheck__ you can override isinstance. It is > possible (for example) to write

Re: Behaviour-based interface/protocol implementation?

2011-01-26 Thread Daniel Urban
> That's just what I'd like and I suppose can't be currently done with > current ABC, PyProtocols or zope.interface implementations, right? It can. With __instancecheck__ you can override isinstance. It is possible (for example) to write a subclass of abc.ABCMeta, which extends __instancecheck__ t

Re: Behaviour-based interface/protocol implementation?

2011-01-26 Thread Alan Franzoni
On Tue, Jan 25, 2011 at 6:48 PM, Terry Reedy wrote: > This is correct! > > print(len(mo)) > TypeError: object of type 'MyObj' has no len() That's interesting. I must admit I was not thinking about special methods in my original post, I used that example just because of Chris response. by the way

Re: Behaviour-based interface/protocol implementation?

2011-01-25 Thread Terry Reedy
On 1/25/2011 10:32 AM, Alan Franzoni wrote: You're right, I forgot about subclass check. But that's really a placebo, because it statically checks the object's *class* for such method, That is exactly the proper check. Instance *methods* are defined on the class. > not the actual instance:

Re: Behaviour-based interface/protocol implementation?

2011-01-25 Thread Alan Franzoni
On Tue, Jan 25, 2011 at 7:55 AM, Chris Rebert wrote: > Not true actually: > > Python 2.7.1 (r271:86832, Dec  5 2010, 00:12:20) > [GCC 4.2.1 (Apple Inc. build 5664)] on darwin > Type "help", "copyright", "credits" or "license" for more information. class MyContainer(object):# no special inheri

Re: Behaviour-based interface/protocol implementation?

2011-01-24 Thread Chris Rebert
On Mon, Jan 24, 2011 at 2:13 AM, Alan Franzoni wrote: > Hello, > I'd like to have a system which lets me do certain actions if the > duck-type of a certain objects matches what I expect, i.e. I'd like to > have a formalization of what it's sometimes done through getattr() > calls: > > if getattr(m

Behaviour-based interface/protocol implementation?

2011-01-24 Thread Alan Franzoni
Hello, I'd like to have a system which lets me do certain actions if the duck-type of a certain objects matches what I expect, i.e. I'd like to have a formalization of what it's sometimes done through getattr() calls: if getattr(myobj, "somemethod", None) is not None: myobj.somemethod(somevalu