3 ms·
Let'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 b
by js2 16y ago
Let'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()