unittest.TestCase, lambda and __getitem__

Steven Bethard steven.bethard at gmail.com
Mon Sep 13 06:32:27 CEST 2004


So, GvR said a few times that he would like to get rid of lambda in
Python 3000.  Not to start up that war again, but I'm trying to
eliminate unnecessary lambdas from my code, and I ran into a case
using unittest.TestCase that I don't really know how to deal with.

Previously, I had written some code like:

self.assertRaises(ValueError, lambda: method(arg1, arg2))

This was a simple fix because assertRaises takes *args and **kwds, so
I fixed it to look like:

self.assertRaises(ValueError, method, arg1, arg2)

which is much cleaner anyway (and what I should have been doing from
the start). Where I get uneasy is when I run into code like:

self.assertRaises(ValueError, lambda: obj[index])

Presumably, I could write this as:

self.assertRaises(ValueError, obj.__getitem__, index)

I guess this makes me uneasy because I'm not entirely certain that
obj[item] is *always* translated to obj.__getitem__(index).  Is it? 
That is, is there any way that obj[item] would get translated into a
different method call?

Or is there a better/clearer way of handling this kind of test case?

Thanks,

Steve
-- 
You can wordify anything if you just verb it.
        - Bucky Katt, Get Fuzzy



More information about the Python-list mailing list