Checking if a variable is a dictionary

Neil Cerutti mr.cerutti at gmail.com
Thu Mar 6 13:17:28 EST 2008


On Thu, Mar 6, 2008 at 12:17 PM, Guillermo
<guillermo.listas at googlemail.com> wrote:
> > You can also get the dynamic polymorphism without invoking inheritance
>  > by specifying a protocol that the values in your dict must implement,
>  > instead. Protocols are plentiful in Python, perhaps more popular than
>  > type hierarchies.
>
>  I'm used to languages with stricter rules than Python. I've read a bit
>  about Python protocols, but where could I get some more info on
>  implementation details? Sounds very interesting.

A protocol is just an interface that an object agrees to implement. In
your case, you would state that every object stored in your special
dict must implement the to_tagged_value method with certain agreeable
semantics.

Python implements sequence types, mapping types, iterators, file
streams, and other things with protocols.

For example, iterators must support the "next" and and "__iter__" methods.

Using a protocol instead instead of a type hierarchy requires less
boring boilerplate, and I suppose in Python will usuallly be
preferable except when you want to inherit implementation as well as
interface. In that case, inheritance often saves boilerplate cide
rather than increases it.

It's also misnomered as duck-typing (clearly it should be nomed quack-typing).

-- 
Neil Cerutti <mr.cerutti+python at gmail.com>



More information about the Python-list mailing list