473,503 Members | 338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
10 3535

<sr****@yahoo.com> wrote in message
news:22**************************@posting.google.c om...
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

Jul 18 '05 #2
sr****@yahoo.com 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
Jul 18 '05 #3
On Tue, Sep 16, 2003 at 02:01:18AM -0700, sr****@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)
Jul 18 '05 #4
"Oren Tirosh" <or*******@hishome.net> wrote ...
On Tue, Sep 16, 2003 at 02:01:18AM -0700, sr****@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]

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

Jul 18 '05 #5
Steve Holden wrote:

exquacktly [ducks and runs]

^^^^^

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

-Peter :-)
Jul 18 '05 #6
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

Jul 18 '05 #7
"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com...
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/

Jul 18 '05 #8
Steve Holden wrote:

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com...
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
Jul 18 '05 #9
On Tue, Sep 16, 2003 at 03:47:11PM +0000, Steve Holden wrote:
"Oren Tirosh" <or*******@hishome.net> wrote ...
On Tue, Sep 16, 2003 at 02:01:18AM -0700, sr****@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

Jul 18 '05 #10
"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com...
Steve Holden wrote:

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:3F***************@engcorp.com...
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/

Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1853
by: atbusbook | last post by:
lets say you want a generic numerical algorithom like sum Ruby def sum lst lst.inject(0){|total,current| total*current} end Java // i dont know if there is a numeric super class for numbers
1
3001
by: Luis Zarrabeitia | last post by:
Hi there. I just tried this test: ==== def f(**kwds): print kwds import UserDict d = UserDict.UserDict(hello="world")
0
847
by: Paddy | last post by:
The official glossary entry here: http://docs.python.org/tut/node18.html#l2h-46 says: " duck-typing Pythonic programming style that determines an object's type by inspection of its method or...
0
763
by: Neville Dempsey | last post by:
Basically I have an existing (maybe a rather large and complicated (existing) instance) that I want to add new member to. Cheers N Hacks/attempts follow: from math import sqrt
16
3432
by: Joe Strout | last post by:
Let me preface this by saying that I think I "get" the concept of duck- typing. However, I still want to sprinkle my code with assertions that, for example, my parameters are what they're...
0
339
by: Tim Rowe | last post by:
2008/11/12 Joe Strout <joe@strout.net>: What do you actually mean by "Quacks like a string"? Supports the 'count()' method? Then you find out if it doesn't when you try to apply the 'count()'...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.