Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5589 > unrolled thread

Re: if statement on lenght of a list

Started byChris Rebert <clp2@rebertia.com>
First post2011-05-17 11:31 -0700
Last post2011-05-17 11:31 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: if statement on lenght of a list Chris Rebert <clp2@rebertia.com> - 2011-05-17 11:31 -0700

#5589 — Re: if statement on lenght of a list

FromChris Rebert <clp2@rebertia.com>
Date2011-05-17 11:31 -0700
SubjectRe: if statement on lenght of a list
Message-ID<mailman.1688.1305657082.9059.python-list@python.org>
On Tue, May 17, 2011 at 11:02 AM, Joe Leonardo
<joe.leonardo@datalogix.com> wrote:
>
> Hey all,
>
> Totally baffled by this…maybe I need a nap. Writing a small function to reject input that is not a list of 19 fields.
>
> def breakLine(value):
>     if value.__class__() != [] and value.__len__() != 19:
>         print 'You must pass a list that contains 19 fields.'
>     else:
>         print 'YAY!'
>
> If I pass:
> breakLine([])
>
> I get:
> YAY!
>
> I expect:
> You must pass a list that contains 19 fields.

Your test should use `or` as opposed to `and`. Since you're indeed
passing in a list, the first part of your condition is False; since
you used `and`, this makes the entire condition False. Therefore, the
else clause ("YAY!") gets executed.

Also, your test is written quite strangely. One would more normally
and straightforwardly write it as:
    if not isinstance(value, list) or len(value) != 19:

Cheers,
Chris
--
http://rebertia.com

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web