unittest.TestCase, lambda and __getitem__

Steven Bethard steven.bethard at gmail.com
Mon Sep 13 03:40:07 EDT 2004


Alex Martelli <aleaxit <at> yahoo.com> writes:
> Steven Bethard <steven.bethard <at> gmail.com> wrote:
> > Or is there a better/clearer way of handling this kind of test case?
> 
> Sure:
> 
>     def wrong_indexing(): return obj[index]
>     self.assertRaises(ValueError, wrong_indexing)

Yeah, I guess I was just begging someone to give me that response. ;)

First, I have to mention that I'd probably have to write your code as

def wrong_indexing():
    return obj[index]
self.assertRaises(ValueError, wrong_indexing)

because GvR has also commented that he wishes he hadn't allowed the one-
line "if: statement" syntax, so by analogy, I assume he'd also rather no one-
line def statement.  So I'm stuck replacing a single line with three...  I was 
hoping to avoid this...  (Of course, if I have a bunch of obj[index] type 
calls, I can average the additional line cost over all of these by making the 
function suitably general.)

Regardless, your analogy between obj[index] and an arbitrary mathematical 
expression was helpful.  It clarifies that, what I really want is an anonymous 
code block...

Hmm...  Well, if this is really the only option, I'll probably leave these 
lambdas until I absolutely have to remove them...

Steve




More information about the Python-list mailing list