[Tutor] recursive problem

Alan Gauld alan.gauld at btinternet.com
Sun Sep 12 21:52:06 CEST 2010


"Roelof Wobben" <rwobben at hotmail.com> wrote

>>> I guess the question to ask/consider is: How can be establish 
>>> whether a
>>> particular object supports a particular interface/set of 
>>> behaviours

In general we "try" it and handle the exceptions

>>> that we require? E.g. how do we most pythonically check whether 
>>> some
>>> object "walks like a list" and "quacks like a list" without tying 
>>> such
>>> code to explicit type checking?

But the risk is that although it may quack, it does so like a missile
launch program rather than a list - oops! :-)

> With the knowlegde I have from the few chapters of thinking like
> a computer scientist I don't know the answer to the last question.

If you do need to avoid accidentally launching missiles then you need
to do some type checking - although ideally using isinstance() instead
of type(). But in the majority of cases some sensible documentation
and Python's culture that we are all adults here usually means you 
don't
need to.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list