Duck Typing

Oren Tirosh oren-py-l at hishome.net
Tue Sep 16 14:12:47 EDT 2003


On Tue, Sep 16, 2003 at 03:47:11PM +0000, Steve Holden wrote:
> "Oren Tirosh" <oren-py-l at hishome.net> wrote ...
> > On Tue, Sep 16, 2003 at 02:01:18AM -0700, srijit at yahoo.com wrote:
> > > Hello All,
> > > I have been seeing this term "duck typing" for quite sometime now. It
> > > will be nice if one of us can give an example in Python demonstrating
> > > duck typing and/or link to some Python references.
> >
> > class Duck:
> >     def quack(self):
> >         print "Quack!"
> >
> > class MeWearingSillyDuckOutfit:
> >     def quack(self):
> >         print "ummm... Quack!"
> >
> > def make_it_quack(obj):
> >     obj.quack()
> >
> > a = Duck()
> > b = MeWearingSillyDuckOutfit()
> > make_it_quack(a)
> > make_it_quack(b)
> >
> 
> exquacktly [ducks and runs]

class Run:
    def quack(self):
       print "QUACK!"

ducks = [Duck() for i in range(10)]
runs = [Run() for i in range(10)]
apply(make_it_quack, ducks+runs)

   Oren





More information about the Python-list mailing list