[Python-ideas] check interfaces, isducktype(X, A)

Serhiy Storchaka storchaka at gmail.com
Mon Dec 2 18:36:09 CET 2013


02.12.13 17:55, Gregory Salvan написав(ла):
> Hi,
> I've made a try of a function that check object/class members for duck
> typing.
>
> For now I've basically called it isducktype(X, A)
> it returns true if:
> - X has all attributes of A
> - X methods have same number of arguments than corresponding A methods
>
> or if A.__ducktypecheck__(X) returns True
>
> Behaviour looks like isinstance and issubclass (support tuples...).

class FooReader:
     def read(self, n=None): ...

class BarReader:
     def read(self, amount=-1): ...

class BazReader:
     def read(self, size=-1, chars=-1, firstline=False): ...

Instances of all three classes can be used where needed an object with 
the read() method which are called with one positional argument or 
without arguments. Is it possible to use isducktype() for such case?




More information about the Python-ideas mailing list