On 8/1/07, beginner <[EMAIL PROTECTED]> wrote: > On Aug 1, 11:28 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote: > > On 8/1/07, beginner <[EMAIL PROTECTED]> wrote: > > > > > > > > > > > > > Hi, > > > > > 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. > > > > > x=[(rec_stdl[0].st/10000.0, > > > rec_stdl[0].cl, > > > rec_stdl[0].bb, > > > rec_stdl[0].bo, > > > rec_stdl[1].bb, > > > rec_stdl[1].bo, > > > rec_stdl[0].ex > > > ) > > > for rec_stdl in rec_by_ex if len(rec_stdl)==2 > > > ] > > > > > #duplicated loop > > > if __debug__: > > > for rec_stdl in rec_by_ex: > > > l=len(rec_stdl) > > > assert(l<=2 and l>0) > > > if l==2: > > > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P") > > > assert(rec_stdl[0].ex==rec_stdl[1].ex) > > > assert(rec_stdl[0].st==rec_stdl[1].st) > > > assert(rec_stdl[0].cp==rec_stdl[1].cp) > > > > 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. > > > Do you mean I should not use the parentheses? >
Yes. > > > 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. > > I know. My original question was how. Dan suggested to write a checker > function. > Use the second block in all cases. In any situation where "if __debug__" is False, the asserts are a noop and in fact won't even be present in the bytecode. > > > Thirdly: This sort of testing is precisely what unit tests and/or > > doctests are for. > > Agreed. > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list