Assertion in list comprehension
stargaming at gmail.com
Wed Aug 1 18:55:53 CEST 2007
On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
> On 8/1/07, beginner <zyzhu2000 at gmail.com> wrote:
>> Does anyone know how to put an assertion in list comprehension? I have
>> the following list comprehension, but I want to use an assertion to
>> check the contents of rec_stdl. I ended up using another loop which
>> essentially duplicates the functions of list comprehension. It just
>> look like a waste of coding and computer time to me.
>> I just wish I could put the assertions into list comprehensions.
>> for rec_stdl in rec_by_ex if len(rec_stdl)==2
>> #duplicated loop
>> if __debug__:
>> for rec_stdl in rec_by_ex:
>> assert(l<=2 and l>0)
>> if l==2:
>> assert(rec_stdl.c=="C" and rec_stdl.c=="P")
> First: All your asserts are wrong. Assert is a statement, not a
> function. These specific ones will behave as expected, but it's easy to
> accidentally write ones that always pass this way.
Could you come up with an example? I can only think of accidentally
injecting a comma, what would create a (true, in a boolean context) tuple.
And, well, if you're only using () for readabilty, this might sometimes
look messy when calling assert with the extended syntax::
assert(False), "error text"
Where one could expect the construction of a tuple.
> Secondly: This is a waste of code, because if __debug__ is not defined
> asserts will be skipped by the compiler. You could use the same loop
> block for both branches.
Well, the `assert` isn't there for no reason, but if you're serious about
it, `raise` could be better.
> Thirdly: This sort of testing is precisely what unit tests and/or
> doctests are for.
Huh? What beginner is doing there seems more like input validation than
testing. Unit or doctests are meant for testing (and in case of doctests,
showing) whether a function works as expected.
More information about the Python-list