[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: expressions vs. statements



kragen wrote:
> But sometimes I write Python code like this test case, which
> demonstrates why I wish I had macros, inline first-class code blocks,
> or the unification of expressions and statements:
> 
>     def testBoundsChecking(self):
>         # other tests like testSetUp check bounds checking on reads
>         def lambdaSucks(self=self): self.lw[4] = 1  # lambda sucks in Python
>         self.failUnlessRaises(IndexError, lambdaSucks)
>         def lambdaStillSucks(self=self): self.lw[-5] = 1
>         self.failUnlessRaises(IndexError, lambdaStillSucks)
>         def lambdaSucksBad(self=self): del self.lw[4]
>         self.failUnlessRaises(IndexError, lambdaSucksBad)
>         def moreLambdaSuckage(self=self): del self.lw[-5]
>         self.failUnlessRaises(IndexError, moreLambdaSuckage)
> 
> Assignment and deletion are two operations in Python that can't be
> done in an expression, and lambda encloses expressions, not
> statements.  So I have to name the statements that should give rise to
> IndexError.  I'd really much rather say
> failUnlessRaises IndexError (lw[4] = 1).

that's why you should use doctest, not unittest ;-)

</F>