[Tutor] Function design
eduardo.susan at gmail.com
Tue Oct 27 19:08:19 CET 2009
On Tue, Oct 27, 2009 at 10:14 AM, Dave Angel <davea at ieee.org> wrote:
> Benno Lang wrote:
>> On Tue, Oct 27, 2009 at 8:21 AM, Dave Angel <davea at ieee.org> wrote:
>>> I agree with Luke's comments. But I'd like to point out an apparent bug
>>> haven't tried the code, this is just by inspection).
>>> You use the test
>>> if '0' in row[.....]
>>> that's not going to check for a zero value, it's going to check for a
>>> digit character somewhere in the value. So 504 would also be recognized,
>>> well as 540.
>> I thought this sounded rather fishy, along the following lines:
>> row is a list, so slicing it returns a list, so 'in' should compare
>> the list elements (i.e. the strings contained therein) with '0'; this
>> seems perfectly normal.
>> I have checked, and thankfully the world is not upside down. It would
>> only work the way you describe if row was a string, or if 'in' called
>> itself recursively when it found a list.
> You are quite right, of course. I somehow missed that it was a slice, and
> took it instead to be a simple subscript.
> But if the file contained 0.00 as one of its items, this code would still
> have the other problem I mentioned.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Thanks for your pointers, Dave. Actually the csv file have data like this:
CAOCA-SD3,OCA - Sign Design Elements Vol. 3,0,0,0,0,0,0,0,0,0,0,0,0
At first I thought that the csv module would do type coercion and my
original code was:
if 0 in row
but nothing was caught.
so I change it to:
if '0' in row
and all was well.
Especially to Luke and Alan:
Yes. A function returning the number of rows with zeros seems like a
I'm enjoying the input from this community. I first started diving
into programming using AutoHotkey, for a lot of automation stuff on
windows. Autohotkey is very much procedure oriented, and you can even
record macros. The scripting language allows functions and several
other advanced features.
Sometimes I have a need and think. Let me quick do this using either
Autohotkey or Python. Sometimes both. I am glad that especially being
exposed to Python I am learning how to design my code for reuse,
making things more flexible, etc.
More information about the Tutor