[Tutor] Truckers Log....Me Again

Paul Sidorsky paulsid@shaw.ca
Tue, 22 Jan 2002 02:29:34 -0700


Erik Price wrote:

> Today I learned about tuples and dictionaries.  I'm not going to come to
> any conclusions about the language until I've gotten to play with it
> some (just too busy with Work at the moment), but I am initially
> surprised by the separate distinction of lists and dictionaries (which,
> unconsciously, I end up replacing with numeric and associative arrays --
> a habit that I am trying to break).

That's a keen observation, considering a dictionary-equivalent could be
implemented with lists.  (That might be a good learning project for
those who were looking for one:  implement a dictionary-like type using
lists, entirely in Python.)  For example:

{"key1": 12, "key2": "hi", key3: [2, 4, 6], key4: (1, 2, 3)} 

could be viewed as:

[["key1", 12], ["key2", "hi"], ["key3", [2, 4, 6]], ["key4", (1, 2, 3)]]

or also this way, which is probably closer to how Python dicts actually
work:

keys = ["key1", "key2", "key3", "key4"]
values = [12, "hi", [2, 4, 6], (1, 2, 3)]

A big problem with using lists is, of course, that access and
manipulation starts to get tricky.  Another problem is you now have to
search to find keys; this may involve traversing most of the list. 
Dictionaries use a hashtable so there is no search time involved (well
except with chaining, but that's normally not a significant factor).

The concept of subscripting with, say, a string (e.g. mydict["key1"])
may seem unusual at first, but it does become natural!  Subscripting
with a more complex object like a tuple or a class is even more bizzare
the first time you do it but it does save tons of work!

> It's not considered "bad programming
> style" to depend heavily on the 'break' statement in Python, is it?

I wouldn't go that far.  If you've got a loop with a lot of breaks in it
then it probably should be redesigned, no matter what language it's
written in.  In these cases the breaks may as well just be gotos -
indeed, often the latter would at least make things more readable. 
However usually, if not always, things can be improved either via a
better design or at worst by using exceptions.

> I'm also a stickler for detail.  A question about quoting -- I have seen
> double and single quotes used fairly interchangeably.  My reference
> doesn't distinguish a difference between these (only that escaping
> singlequotes inside doublequotes or vice versa isn't necessary, or in
> triple (single|double) quotes).  But in PHP/Perl/bash, variables expand
> in double-quotes so the difference is important.  Where does Python
> stand on this subject?  (I'm surprised that I didn't read something
> about this already.)

Check the Python Style Guide (PEP 8) if you want to know the "official"
policy.  

For me, as a C (and C++) geek, I use C-like quoting methods.  I use "
for strings and ' for single characters, unless I need the " character
in a string in which case I probably will use ' instead.  I also use r"
for regexps, and """ for docstrings or large display screens.  I don't
use ''', at least not yet.  (Actually, the use of ''' seems to be
extemely rare from what I have seen of other people's code.)

For yourself, just try to adopt something that's consistent and
comfortable, and revel in the fact that in Python you have the freedom
to choose.  :-)

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/