Duck Typing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srijit@yahoo.com

    Duck Typing

    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
  • John Roth

    #2
    Re: Duck Typing


    <srijit@yahoo.c om> wrote in message
    news:221d8dbe.0 309160101.4a09f 2ad@posting.goo gle.com...[color=blue]
    > 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.[/color]

    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
    [color=blue]
    >
    > Regards,
    > Srijit[/color]


    Comment

    • Irmen de Jong

      #3
      Re: Duck Typing

      srijit@yahoo.co m wrote:[color=blue]
      > 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.[/color]

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

      --Irmen de Jong


      Comment

      • Oren Tirosh

        #4
        Re: Duck Typing

        On Tue, Sep 16, 2003 at 02:01:18AM -0700, srijit@yahoo.co m wrote:[color=blue]
        > 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.[/color]

        class Duck:
        def quack(self):
        print "Quack!"

        class MeWearingSillyD uckOutfit:
        def quack(self):
        print "ummm... Quack!"

        def make_it_quack(o bj):
        obj.quack()

        a = Duck()
        b = MeWearingSillyD uckOutfit()
        make_it_quack(a )
        make_it_quack(b )


        Comment

        • Steve Holden

          #5
          Re: Duck Typing

          "Oren Tirosh" <oren-py-l@hishome.net> wrote ...[color=blue]
          > On Tue, Sep 16, 2003 at 02:01:18AM -0700, srijit@yahoo.co m wrote:[color=green]
          > > 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.[/color]
          >
          > class Duck:
          > def quack(self):
          > print "Quack!"
          >
          > class MeWearingSillyD uckOutfit:
          > def quack(self):
          > print "ummm... Quack!"
          >
          > def make_it_quack(o bj):
          > obj.quack()
          >
          > a = Duck()
          > b = MeWearingSillyD uckOutfit()
          > make_it_quack(a )
          > make_it_quack(b )
          >[/color]

          exquacktly [ducks and runs]

          regards
          --
          Steve Holden http://www.holdenweb.com/
          Python Web Programming http://pydish.holdenweb.com/pwp/



          Comment

          • Peter Hansen

            #6
            Re: Duck Typing

            Steve Holden wrote:[color=blue]
            >
            > exquacktly [ducks and runs][/color]
            ^^^^^

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

            -Peter :-)

            Comment

            • Irmen de Jong

              #7
              Re: Duck Typing

              Peter Hansen wrote:[color=blue]
              > Steve Holden wrote:
              >[color=green]
              >>exquacktly [ducks and runs][/color]
              >
              > ^^^^^
              >
              > Steve, did you even _know_ you did this? You're sick!
              >
              > -Peter :-)[/color]

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

              --Irmen

              Comment

              • Steve Holden

                #8
                Re: Duck Typing

                "Peter Hansen" <peter@engcorp. com> wrote in message
                news:3F6732DB.F 31A954E@engcorp .com...[color=blue]
                > Steve Holden wrote:[color=green]
                > >
                > > exquacktly [ducks and runs][/color]
                > ^^^^^
                >
                > Steve, did you even _know_ you did this? You're sick!
                >
                > -Peter :-)[/color]

                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/



                Comment

                • Peter Hansen

                  #9
                  Re: Duck Typing

                  Steve Holden wrote:[color=blue]
                  >
                  > "Peter Hansen" <peter@engcorp. com> wrote in message
                  > news:3F6732DB.F 31A954E@engcorp .com...[color=green]
                  > > Steve Holden wrote:[color=darkred]
                  > > >
                  > > > exquacktly [ducks and runs][/color]
                  > > ^^^^^
                  > >
                  > > Steve, did you even _know_ you did this? You're sick!
                  > >
                  > > -Peter :-)[/color]
                  >
                  > Aah, I see, you think I'm stupid.[/color]

                  No, I just knew Irmen was asleep at his keyboard. ;-)
                  [color=blue]
                  > Of course I knew - without the bracketed afterthought it would hardly have
                  > been worth posting.
                  >
                  > I'll send you a bill ;-)[/color]

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

                  -Peter

                  Comment

                  • Oren Tirosh

                    #10
                    Re: Duck Typing

                    On Tue, Sep 16, 2003 at 03:47:11PM +0000, Steve Holden wrote:[color=blue]
                    > "Oren Tirosh" <oren-py-l@hishome.net> wrote ...[color=green]
                    > > On Tue, Sep 16, 2003 at 02:01:18AM -0700, srijit@yahoo.co m wrote:[color=darkred]
                    > > > 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.[/color]
                    > >
                    > > class Duck:
                    > > def quack(self):
                    > > print "Quack!"
                    > >
                    > > class MeWearingSillyD uckOutfit:
                    > > def quack(self):
                    > > print "ummm... Quack!"
                    > >
                    > > def make_it_quack(o bj):
                    > > obj.quack()
                    > >
                    > > a = Duck()
                    > > b = MeWearingSillyD uckOutfit()
                    > > make_it_quack(a )
                    > > make_it_quack(b )
                    > >[/color]
                    >
                    > exquacktly [ducks and runs][/color]

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

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

                    Oren

                    Comment

                    • Steve Holden

                      #11
                      Re: Duck Typing

                      "Peter Hansen" <peter@engcorp. com> wrote in message
                      news:3F6749B6.7 4601D54@engcorp .com...[color=blue]
                      > Steve Holden wrote:[color=green]
                      > >
                      > > "Peter Hansen" <peter@engcorp. com> wrote in message
                      > > news:3F6732DB.F 31A954E@engcorp .com...[color=darkred]
                      > > > Steve Holden wrote:
                      > > > >
                      > > > > exquacktly [ducks and runs]
                      > > > ^^^^^
                      > > >
                      > > > Steve, did you even _know_ you did this? You're sick!
                      > > >
                      > > > -Peter :-)[/color]
                      > >
                      > > Aah, I see, you think I'm stupid.[/color]
                      >
                      > No, I just knew Irmen was asleep at his keyboard. ;-)
                      >[color=green]
                      > > Of course I knew - without the bracketed afterthought it would hardly[/color][/color]
                      have[color=blue][color=green]
                      > > been worth posting.
                      > >
                      > > I'll send you a bill ;-)[/color]
                      >
                      > If you make it a twenty, would that be a "sawduck"?
                      >[/color]

                      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/



                      Comment

                      Working...