Duck Typing

John Roth newsgroups at jhrothjr.com
Tue Sep 16 06:53:51 EDT 2003


<srijit at yahoo.com> wrote in message
news:221d8dbe.0309160101.4a09f2ad at posting.google.com...
> 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






More information about the Python-list mailing list