Wiki

Clone wiki

ducktypes / Home

Welcome

ducktypes is a duck typing support library.

It is designed to let the user work with objects depending on their current runtime behaviour, not their class or their register-based (e.g. ABC, zope.interface) interface.

Whet your appetite

from ducktypes.ducktype import Duck

class MyObject(object):
    def some_method(self, a, b):
        pass
    def another_method(self, x):
        pass


class AnotherObject(object):
    def some_method(self, a, b):
        pass
    def another_method(self, a):
        pass

class AnotherDifferentObject(object):
    def some_method(self, a, b, c):
        pass
    def another_method(self, a):
        pass

duck = Duck(MyObject())

print "AnotherObject instances implement MyObject interface: ", duck.maybe_implemented_by(AnotherObject())
print "AnotherDifferentObject instances implement MyObject interface:", duck.maybe_implemented_by(AnotherDifferentObject())


OUTPUT:

AnotherObject instances implement MyObject interface:  True
AnotherDifferentObject instances implement MyObject interface: False

More

ducktypes supports checking for special methods, classmethods, static methods, function attributes; the only thing that matters to it is the way an object *behaves* at runtime.

If you're tired of doing getattr(obj, "some_method", False) calls in your code, that's what you need.

See the README for more info and to learn about its caveats.

Discussion

Discussion is hosted at the pydenji-users mailing list / google group:

http://groups.google.com/group/pydenji-users

Issues

Tickets can be opened through the bitbucket bugtracker

Updated