[CentralOH] Zed's ex39
jep200404 at columbus.rr.com
jep200404 at columbus.rr.com
Tue Nov 25 15:15:45 CET 2014
On Mon, 24 Nov 2014 17:14:15 -0800, timothy spencer <smashing_good_show at hotmail.com> wrote:
> I am on Zed Shaw's book in EX 39, ...
A URL for that is:
> What is a 'bucket'?
> Is a bucket simply the slot where a pair would go?
The artful terms bucket and slot should be defined before they
are used. That they are not is an example of how the on-line
tutorials have weaknesses. You should document them in your
source code and bring it to a dojo for discussion and rework.
The first paragraph on page 25 of Learning Python 5th edition
says in part:
When people write software, they are not writing it for
themselves. In fact, they are not even writing primarily
for the computer. Rather good programmers know tha the
code is written for the next human being who has to read
it in order to maintain or reuse it. If that person cannot
understand the code, it's all but useless in a realistic
development scenario. In other words, programming is not
about being clever and obscure - it's about how clearly
your program communicates its purpose.
> I know that a dictionary is a list of 'key: value' pairs.
That's almost correct. A Python list is ordered.
A Python dictionary is not ordered,
so a Python dictionary is not a list.
> Like if a dictionary is horizontal, then I am wondering if a
> bucket then is a vertical slot that can have lots of tuples.
Draw me a picture. ASCII art is good.
> ... and his description is a little odd.
His code for hashmap.py is mediocre. There may be good reasons
for the mediocrity in a tutorial, such as serving as an example
of how to do things awkwardly, then showing the better way later.
Does the author improve the code later?
Here are some of the things I notice.
That get_slot() returns a copy of the key argument seems
superfluous. The key return value is not used in the two
places that call get_slot(). So don't return the key.
That line 4 of hashmap.py uses range(0, num_buckets) instead
of range(num_buckets) is odd. What rationale can you think of
for using range(0, num_buckets) instead of range(num_buckets)?
I would just use range(num_buckets).
It's curious that he uses range() on line 4, but xrange() on
line 53. What's the rationale for using one instead of the other?
The get_bucket() call on line 39 is redundant with the
get_bucket() call on line 23 when get_slot is called on
line 40. I would have get_slot() return bucket in its
return tuple, and delete line 39.
How would the code work if line 57 was a return instead of a break?
Which do you prefer? Why? Compare that with line 28.
Compare those with using an else: clause for the for loop.
What do you think of line 62? Is it necessary?
Does it show that the author does not know how Python
"does nothing" gracefully? Is the author saving this
clutziness for later refactoring or does the author
just write mediocre code?
Line 25 uses enumerate(). Could you use enumerate()
to combine lines 53 and 54? Would that be an
improvement or make things worse?
Is xrange(len(foo)) ugly or Pythonic?
What should delete() do if no "slot" has the key?
What's a better name for get()? How about get_value?
bucket_id on line 16 is used as an index.
I.e., bucket_id is not an ID.
It's a feeble name. A better name would be bucket_index.
What name is better yet?
Most of the free on-line tutorials have weaknesses.
I would say that the documentation is a bit thin,
and his code should be refactored. You should always
be looking for ways that the code could be improved.
What other opportunities for improvement do you see?
Much code in the real world is worse.
On Mon, 24 Nov 2014 21:58:04 -0500, pybokeh <pybokeh at gmail.com> wrote:
> here is a video that I thought was helpful by a great speaker
> / Python core developer named Raymond Hettinger:
That's a fun video. It covers some of the things I did.
How would one change get_slot() after my changes above
and after watching the video?
More information about the CentralOH