67.9 Avoiding Global Lookups and Repeated Attribute Access
Alright, let’s talk about one of the most common, and frankly, easiest-to-fix performance drains I see in the wild: the double-whammy of global lookups and repeated attribute access. This isn’t about fancy algorithmic wizardry; this is about cleaning up the sloppy, lazy code we all write at 2 AM so it doesn’t embarrass us in the light of day. Think of your code’s namespace as a series of concentric circles. The innermost circle is your local scope. It’s the VIP lounge—getting in there is fast, cheap, and easy. The outermost circle is the global scope, which includes built-in names like len or str. That’s the parking lot. Every time you reference a name in that global scope, the interpreter has to leave the comfy VIP lounge, trudge out to the parking lot, and yell for it. This process—a global lookup—isn’t cripplingly slow, but do it enough times inside a tight loop and it starts to add up like a bar tab at a developer conference.