Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5589
| References | <8F54BE3F56F7A64ABDC3A8164CC83FFA0AB4A096A9@VA3DIAXVS331.RED001.local> |
|---|---|
| Date | 2011-05-17 11:31 -0700 |
| Subject | Re: if statement on lenght of a list |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1688.1305657082.9059.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: if statement on lenght of a list Chris Rebert <clp2@rebertia.com> - 2011-05-17 11:31 -0700
csiph-web