3 ms·
Interesting article, but I did wonder what he means with this: Duck typing allows to reduce immensely the coupling between your classes Can anybody explain?
by st0p 15y ago
Interesting article, but I did wonder what he means with this: Duck typing allows to reduce immensely the coupling between your classes
Can anybody explain?
- js2 15y agoLet's say that I have a list of objects of mixed type, and I only care that they implement a given method. With duck typing, the types of the objects need not be coupled via a common base class (or even interface). For example: # Less coupled class A: def foo(self): print "A" class B: def foo(self): print "B" for x in (A(), B()): x.foo() vs: # More coupled class Base: def foo(self): pass class A(Base): def foo(self): print "A" class B(Base): def foo(self): print "B" for x in (A(), B()): x.foo()