On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote: > On 2007-09-10, Chris Mellon <[EMAIL PROTECTED]> wrote: > > On 9/10/07, Neil Cerutti <[EMAIL PROTECTED]> wrote: > >> On 2007-09-08, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote: > >> > Lawrence D'Oliveiro wrote: > >> >>>>> if y[0]: > >> >>>> Not a good idea. > >> >>> Why not? > >> >> > >> >> Because there is a situation where your version of the test > >> >> will fail even if the first element of y is non-null. > >> > > >> > Such as? Seriously people, a little more verbosity wouldn't > >> > hurt here. This isn't a mystery game. > >> > >> >>> if "": True > >> ... > >> >>> if 0: True > >> ... > >> >>> if []: True > >> ... > >> >>> if {}: True > >> ... > >> >>> if None: True > >> ... > >> > >> That's may not be all the possibilities. Lots of Python objects > >> can evaluate to false in a boolean context. > > > > All of these except 0 and None will also return False if you > > check the length. In fact, I am not aware of any builtin where > > len() will return 0, but it will be true in a boolean context. > > > > If you're advocating being more specific in the hope of > > catching errors, this is a pretty terrible example. The OP > > wasn't checking for "null", whatever that means - he was > > checking for the empty string, in a list known (or intended) to > > contain only strings. The simple boolean check is totally > > appropriate. > > Agreed; but I prefer 'if y[0] == ""', absent more context and > better names. >
Probably should use u"" if you're going to take that route, as this will fail spuriously if y[0] contains a unicode string that can't be implicitly converted to ascii. Personally, I prefer the boolean check and I'll let operations fail elsewhere if there's a type mismatch. -- http://mail.python.org/mailman/listinfo/python-list