When looking at Python code I often see things I don't understand even though I've read an entire introductory book on Python. And while looking through the official documentation can help, it would be beneficial to have a textbook with examples appropriate to my level and exercises to work on. Examples of more advanced things I've seen but don't really understand include iterators, generators, the yield command, ast, useful modules like re, and the assert command.
What's a good 2nd book on Python?
Added 3+ months ago:
Other examples of possible advanced topics that have since come to mind include: namespaces, lambda operators, decorators, how does super() work when there are multiple base classes?, and how to define a class that can use a notation like var[0] like lists do. Note that I don't necessarily need a book that covers everything I've listed, but is there a resource for learning at least some of these topics (potentially along with things I've never heard of) suitable for someone whose already read an introductory book but is not a coding guru?
Answers (1)
You haven't exactly stated your level / experience ("not a coding guru?").
The tutorial with some experimentation ought to clarify some things to a somewhat seasoned programmer, methinks. I'm a mathematician. The more advanced features might only confuse a novice, being unfamiliar with the situations begetting their convenience.
But let's see...
You mean iterators, as in for loop over an object, I presume. If you understand regular for loops, it's just a shortcut - rather than explicitly loop over an object's property, you define that you want to loop over it by default (for example, each char in a string).
Yield and generators are the technical aspect of the iterator. Rather than have to manage the state of the loop manually, you utilise a generator, which is most easily defined as a regular function with a yield - when reaching this statement, ere activating return the function's state is frozen (all parameters are saved on the side, plus current line of code); therefore, when calling the generator again (as a for loop does automatically), you can pick up the next value, and so forth, until it's exhausted. I've used it for example in BFS / DFS of a tree class.
Haven't used ast.
To use re adequately, you must master regular expressions, which is no small feat. It's a form of smart string search which you usually need for a specific purpose of analysing long text (eg an html page, error log) over the regular find. Re has a slightly bothersome structure but only had to write the boilerplate once.
Assert is shorthand for "if (condition) then pass else raise AssertionError". Nothing special about it.
Namespaces - A neat feature, but not one you really use actively. What's relevant to you is that when you define variables in a function, normally you can use any name you like since they're defined in another part of memory, you could say; and base input parameters (other than list, classes and other complex structures which are passed byref) take a temporary value so you can change them freely without affecting the caller value, but if you do want to change them you have to set them as the output parameter (eg, foo(a,b) -> (...) return (a,b)).
Lambda operators - meh. Shorthand for function definition, usually oneliners. Probably most useful for making a function you had defined single parameter to pass to a some method which calls it repeatedly as the value changes, or summat.
Decorators - don't care.
Super - there's an answer for that on stackoverflow, as is often the case. If you know when to use super I'd say you're golden.
List imitation - that's a pretty good one. Override the methods getitem and setitem.
Google is your friend. You know how to phrase the problem, someone will have posted (or written) how best to approach it. I think there's a 100k libraries? Easier to focus on the ones you need. Not much of a bookworm myself.