[Chicago] newbie says HI; needs help

Atul Varma varmaa at gmail.com
Fri Jan 20 17:17:53 CET 2006


On 1/20/06, Andrew Dudzik <adudzik at gmail.com> wrote:
> The problem with learning Python from a Pascal book is that you're going to
> miss all of the syntactical shortcuts that makes Python powerful.  For
> instance, you could use the line 'if texture in ["flaky","caked"]'.  There's
> definitely a good one-liner for this, (the veterans should chime in here)
> but the best I could come up with was this one: (it's a hack)
>
> stock = 2 - (texture in ["flaky","caked"])

This sort of gets to the distinction of "readability" versus
"terseness".  Even though that "hack" is just one line, it's much
harder to read and comprehend than the four line version that Andrew
mentioned earlier.

Instead of separating the "stock" into classifications of "1" or "2",
though, you might instead want to consider creating one boolean
category: for instance, "isFlakyOrCaked", or "isStockOne".  That way
you could convert this into one line of code without sacrificing
readability:

isStockOne = texture in ["flaky", "caked"]

This could lead to problems, though, when you need to write
conditionals later on down the line--for instance, "if not isStockOne"
may be harder to read than "if stock == 2".  Really, I think Andrew's
original four-line solution is fine because it's very easy to read and
isn't so verbose as to be cumbersome.  The bottom line, though, is
that just because Python has lots of language features to make code
really terse doesn't mean you should necessarily use them if it makes
the code harder to understand.

I also think that reading a fun book on Pascal to learn the basics of
programming in Python is fine, because those fundamentals don't vary
too much between the two languages.  In some ways it's even better
because there's the additional task of translating from Pascal to
Python, which makes the learning process more active and interesting. 
It's too bad there aren't any books that teach Python in that sort of
"Sherlock Holmes mystery" setting, though--I miss that kind of
approach to programming, which seems to have disappeared in the last
decade or two.

Anyhow, I hope that helps.

- Atul


More information about the Chicago mailing list