[Python-ideas] check interfaces, isducktype(X, A)
Chris Angelico
rosuav at gmail.com
Mon Dec 2 17:22:51 CET 2013
On Tue, Dec 3, 2013 at 3:11 AM, Oleg Broytman <phd at phdru.name> wrote:
> On Mon, Dec 02, 2013 at 04:55:22PM +0100, Gregory Salvan <apieum at gmail.com> wrote:
>> For now I've basically called it isducktype(X, A)
>> it returns true if:
>> - X has all attributes of A
>
> Isn't the requirement too strong? When I need a file-like object I
> seldom need more than .read() and .write() methods, and even of those I
> seldom need both at once -- I usually need one or the other.
> Testing for strict conformance is not duck typing IMO -- it's
> interface.
But you could have a "ReadableFile" that has a .read() method and
whatever else you need, thus using that class as a sort of interface.
With a little careful scripting and introspection, you might even be
able to craft that by source code analysis, which would be extremely
cool (eg you run this script over your source code and it notes that
elements of this list are indexed with these five keywords, so it
constructs a class with those five so you can check what goes into the
list). Whether it's actually _useful_ or not remains to be seen, but
it would certainly be cool.
ChrisA
More information about the Python-ideas
mailing list