Duck Typing

S

srijit

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
 
J

John Roth

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
 
O

Oren Tirosh

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)
 
S

Steve Holden

Oren Tirosh said:
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
 
I

Irmen de Jong

Peter said:
Steve said:
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
 
S

Steve Holden

Peter Hansen said:
Steve said:
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
 
P

Peter Hansen

Steve said:
Peter Hansen said:
Steve said:
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
 
O

Oren Tirosh

Oren Tirosh said:
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
 
S

Steve Holden

Peter Hansen said:
Steve said:
Peter Hansen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,440
Latest member
in3dagenonline

Latest Threads

Top