Checking if a variable is a dictionary

castironpi at castironpi at
Sun Mar 9 21:58:15 CET 2008

On Mar 9, 9:23 am, Steven D'Aprano <st... at REMOVE-THIS-> wrote:
> On Sun, 09 Mar 2008 06:58:15 -0700, Guillermo wrote:
> > Okay, so I think I know where's the catch now -- you must rely on the
> > fact that the protocol is implemented, there's no way to enforce it if
> > you're expecting a parrot-like object. You'd try to call the speak()
> > method and deal with the error if there's no such method?
> That's right. That's called "duck typing" -- if all you want is something
> that quacks like a duck, then it doesn't matter if it actually is a duck
> or not.
> Or if you prefer: if it quacks like a duck and swims like a duck, then
> it's close enough to a duck as to make no difference.

The syntax checker over here raised a warning on this one.  "So close
to a duck as."  (I'm using the construct in my Propositional
Calculus program.)

> try:
>     something.speak
> except AttributeError:
>     # No speak() method, so it can't be a parrot.
>     do_something_else()

It might be a dead parrot.  HAR!

> else:
>     # It seems to follow the parrot protocol.
>     yummy_goodness = something.speak(5)
>     assert "spam" in yummy_goodness.lower()

P.S.  Is 'resemblant' a word?  So resemblant of a duck as.

More information about the Python-list mailing list