On Jul 30, 5:09 am, Maric Michaud <[EMAIL PROTECTED]> wrote: > Le Tuesday 29 July 2008 23:48:31 [EMAIL PROTECTED], vous avez écrit : > > def print_members(iterable): > > if not iterable: > > print '<members />' > > return > > print '<members>' > > for item in iterable: > > print '<item>%s</item>' % item > > print '</members>' > > But iterables can have no notion of emptiness too : > > >>>[25]: print_members((e for e in range(0))) > > <members> > </members>
Ack! Maybe I meant "container" rather than "iterable". Or maybe I'd be wrong on that count, too. Other people have come up with better examples, so I won't try to fix my hasty code (although I'll keep that in mind if I have to write a custom XML writer). > > class Signal: > > [...] > > def dc_offset(self, amount): > > if amount == 0: > > return > > self.samples = [sample + amount for sample in self.samples] > > This function is also wrong assuming that because amount compare to zero, it > can be added to sample. Not quite. I'm assuming that if amount equals 0, then amount is some scalar. In fact, only if that comparison fails will I get my exception: since [] != 0, it will then try to add sample + [], which will raise TypeError. > If you want to make type checking just check the type or convert your > parameter to an int, but the test "== 0" is of no help here. I'm not going for type checking here because I want Signal to support int and float samples (and numpy.int16, &c.). > The only valuable point I see for this idiom is to make more explicit I am > testing a numerical value. That's one of the reasons I wrote it that way. Signal has other methods that take lists (like mix and envelope), which I could get confused with the ones that take scalars (offset and change volume). Cheers, Geoff G-T -- http://mail.python.org/mailman/listinfo/python-list