In the specific case of hashlib, @dtrodrigues has given the best possible answer; but in general, there’s also another thing you can do. Every time you do thing.attribute, you can rewrite that as getattr(thing, "attribute") - and since the attribute name is just a string now, that can come from a variable. VERY useful when iterating over the dir() of a module or object!
Can you recommend a good python reference book? At the moment I’m using Python Crash Course by Eric Matthes, which is very up to date, but doesn’t cover very much. It doesn’t mention getattr, for example.
Dead-tree book? No, sorry; haven’t delved into them so I can’t make any recommendations (I know there are some good ones out there, but I’m not going to recommend something I don’t personally know).
What I tend to point people to is the main Python tutorial, and for web programming, the Django Girls tutorial. Both are excellent. (Coding is definitely for girls, and also for people who don’t consider themselves girls, and I recommend that tutorial regardless of someone’s gender.)