verify the return value of a function

Chris Rebert clp2 at rebertia.com
Thu Jan 19 16:13:22 EST 2012


On Thu, Jan 19, 2012 at 12:45 PM, Jabba Laci <jabba.laci at gmail.com> wrote:
> Hi,
>
> In a unit test, I want to verify that a function returns a
> cookielib.LWPCookieJar object. What is the correct way of doing that?
<snip>
> 3) isinstance(return_value, cookielib.LWPCookieJar) seems to be the
> best way, however somewhere I read that using isinstance is
> discouraged

Explicit typechecking is often discouraged in favor of duck typing.
However, if you want to do explicit typechecking (as one might in unit
tests), then isinstance() is absolutely the technique to use.
The alternative would be to check for the specific attributes of
LWPCookieJar that you're relying upon (using hasattr() or similar),
but that's probably overkill here.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list