@Serhiy
Not really (only with FooReader, BarReader) as it checks if functions co_argcount and co_kwonlyargcount are equals.
In this case it requires BazReader.__ducktypecheck__ to be implemented.

I had a doubt when doing this, I agree with you isducktype(BazReader, FooReader) can return True (whereas  isducktype(FooReader, BazReader) should stay false)
This can easily be changed.

@Oleg:
The fact there is a lot of interface implementations means certainly it's something needed.
Each implementation I've seen add burden on objects whereas the only thing personnally I need, is
to be sure I'll be able to call some methods off injected objects.

@spir:
Names can easily be checked, but I can't see why doing this, it add constraints to developpers without adding safety.

@Andrew +1
Maybe inspect is more appropriate as it provides ArgSpec and getmembers ?

My code is crappy, I've just do it like that to train me in C then tought it might be interesting to get feedback.


2013/12/2 Serhiy Storchaka <storchaka@gmail.com>
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?



______________________________
_________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas