Duck Typing

Discussion in 'Python' started by srijit@yahoo.com, Sep 16, 2003.

  1. Guest

    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.

    Regards,
    Srijit
     
    , Sep 16, 2003
    #1
    1. Advertising

  2. John Roth Guest

    <> wrote in message
    news:...
    > 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.


    Why in the world would anyone want to do such a thing? I hadn't
    heard the term until you just brought it up, so a quick Google defined
    it. AFAICT, it's simply a cute name that's applied to a very common
    technique in languages that don't use static typing.

    In other words, if you need a file-like object, and someone passes
    you an object, your options are either to just use it (and handle the
    exceptions if it doesn't really support the proper interface) or use
    reflection to see if it has methods with the proper name and number
    of parameters, and then be prepared to handle the exceptions when
    they don't do what you expect.

    Six of one, half a dozen of the other.

    John Roth

    >
    > Regards,
    > Srijit
     
    John Roth, Sep 16, 2003
    #2
    1. Advertising

  3. wrote:
    > 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.


    Is this helpful: http://www.razorvine.net/python/PythonLanguageConcepts
    (paragraph 'dynamic typing') ?

    --Irmen de Jong
     
    Irmen de Jong, Sep 16, 2003
    #3
  4. Oren Tirosh Guest

    On Tue, Sep 16, 2003 at 02:01:18AM -0700, 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)
     
    Oren Tirosh, Sep 16, 2003
    #4
  5. Steve Holden Guest

    "Oren Tirosh" <> wrote ...
    > On Tue, Sep 16, 2003 at 02:01:18AM -0700, 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]

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
     
    Steve Holden, Sep 16, 2003
    #5
  6. Peter Hansen Guest

    Steve Holden wrote:
    >
    > exquacktly [ducks and runs]

    ^^^^^

    Steve, did you even _know_ you did this? You're sick!

    -Peter :)
     
    Peter Hansen, Sep 16, 2003
    #6
  7. Peter Hansen wrote:
    > Steve Holden wrote:
    >
    >>exquacktly [ducks and runs]

    >
    > ^^^^^
    >
    > Steve, did you even _know_ you did this? You're sick!
    >
    > -Peter :)


    people are currently turning their heads to me to see why I
    suddenly bursted out in laughter :)

    --Irmen
     
    Irmen de Jong, Sep 16, 2003
    #7
  8. Steve Holden Guest

    "Peter Hansen" <> wrote in message
    news:...
    > Steve Holden wrote:
    > >
    > > exquacktly [ducks and runs]

    > ^^^^^
    >
    > Steve, did you even _know_ you did this? You're sick!
    >
    > -Peter :)


    Aah, I see, you think I'm stupid.

    Of course I knew - without the bracketed afterthought it would hardly have
    been worth posting.

    I'll send you a bill ;-)

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
     
    Steve Holden, Sep 16, 2003
    #8
  9. Peter Hansen Guest

    Steve Holden wrote:
    >
    > "Peter Hansen" <> wrote in message
    > news:...
    > > Steve Holden wrote:
    > > >
    > > > exquacktly [ducks and runs]

    > > ^^^^^
    > >
    > > Steve, did you even _know_ you did this? You're sick!
    > >
    > > -Peter :)

    >
    > Aah, I see, you think I'm stupid.


    No, I just knew Irmen was asleep at his keyboard. ;-)

    > Of course I knew - without the bracketed afterthought it would hardly have
    > been worth posting.
    >
    > I'll send you a bill ;-)


    If you make it a twenty, would that be a "sawduck"?

    -Peter
     
    Peter Hansen, Sep 16, 2003
    #9
  10. Oren Tirosh Guest

    On Tue, Sep 16, 2003 at 03:47:11PM +0000, Steve Holden wrote:
    > "Oren Tirosh" <> wrote ...
    > > On Tue, Sep 16, 2003 at 02:01:18AM -0700, 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
     
    Oren Tirosh, Sep 16, 2003
    #10
  11. Steve Holden Guest

    "Peter Hansen" <> wrote in message
    news:...
    > Steve Holden wrote:
    > >
    > > "Peter Hansen" <> wrote in message
    > > news:...
    > > > Steve Holden wrote:
    > > > >
    > > > > exquacktly [ducks and runs]
    > > > ^^^^^
    > > >
    > > > Steve, did you even _know_ you did this? You're sick!
    > > >
    > > > -Peter :)

    > >
    > > Aah, I see, you think I'm stupid.

    >
    > No, I just knew Irmen was asleep at his keyboard. ;-)
    >
    > > Of course I knew - without the bracketed afterthought it would hardly

    have
    > > been worth posting.
    > >
    > > I'll send you a bill ;-)

    >
    > If you make it a twenty, would that be a "sawduck"?
    >


    You'll never beat me at this game. After all, I did write "Web Programming
    in Python" ;-)

    regards
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/pwp/
     
    Steve Holden, Sep 17, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    575
    Isaac Gouy
    Sep 1, 2006
  2. Replies:
    2
    Views:
    327
    Terry Reedy
    Jan 28, 2008
  3. Paddy
    Replies:
    1
    Views:
    406
    Paddy
    May 9, 2008
  4. Paddy
    Replies:
    0
    Views:
    244
    Paddy
    Jun 25, 2008
  5. Jose Mora

    duck typing at will

    Jose Mora, Dec 30, 2008, in forum: Python
    Replies:
    1
    Views:
    276
    Bruno Desthuilliers
    Dec 30, 2008
Loading...

Share This Page