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 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
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)
"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/
Steve Holden wrote: exquacktly [ducks and runs]
^^^^^
Steve, did you even _know_ you did this? You're sick!
-Peter :-)
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
"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/
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
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
"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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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")
|
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...
|
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
|
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...
| |
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()'...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |