[Tutor] Poor style to use list as "array"?
Angus Rodgers
angusr at bigfoot.com
Mon Jul 6 17:48:13 CEST 2009
On Mon, 6 Jul 2009 01:51:22 +0100, Rich Lovely wrote:
[I wrote:]
>> if name in plural:
>> name = plural[name]
>> else:
>> name += 's'
>This could be written more cleanly (although arguably not as readably) as
>
>name = plural.get(name, name + "s")
Nice, and readable enough, I think.
>d.get(key, default) returns the value from d mapped to key if it
>exists, or default otherwise.
>
>You might also want to split your calculation and display code into
>two separate loops. This might seem wasteful, but it will make your
>code easier to read and maintain, and the waste is only marginal with
>the loops you're running - there is a maximum of only 17 passes (once
>for each value of coin and note)
If I understand you correctly, this is the same idea as Bob Gailer
used in his code - in which 'buff' becomes a list, instead of (as
in my code) a string, formatted for printing. It certainly seems
to simplify the whole thing enormously:
<http://python.pastebin.com/d42610591> (retention: 1 day)
(Ignore the boilerplate!)
Thanks to all of you. I haven't adopted the suggestion of using
classes, which I think I'll leave for when I'm reading the later
chapters of the book (although I do get the gist of the idea).
--
Angus Rodgers
More information about the Tutor
mailing list