Title: Python Closures — Late Binding Loop Bug in Rate Limiters
Open Graph Title: Python Closures — Late Binding Loop Bug in Rate Limiters
Description: All endpoints returned 429 after one hit its limit—closures captured loop variables by reference. Fix late binding and inspect __closure__ cells.
Open Graph Description: All endpoints returned 429 after one hit its limit—closures captured loop variables by reference. Fix late binding and inspect __closure__ cells.
Opengraph URL: https://thecodeforge.io/python/closures-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/closures-python/"
},
"headline": "Python Closures — Late Binding Loop Bug in Rate Limiters",
"description": "All endpoints returned 429 after one hit its limit—closures captured loop variables by reference. Fix late binding and inspect __closure__ cells.",
"keywords": "Python Closures — Late Binding Loop Bug in Rate Limiters, Python, Intermediate, Functions, All endpoints returned 429 after one hit its limit—closures captured loop variables by reference. Fix late binding and inspect __closure__ cells.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-23T00:00:00+00:00",
"url": "https://thecodeforge.io/python/closures-python/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python Closures — Late Binding Loop Bug in Rate Limiters", "item": "https://thecodeforge.io/python/closures-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is a closure in Python and how is it different from a regular nested function?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A regular nested function is just a function defined inside another function. A closure is a nested function that references at least one variable from the enclosing function's scope AND is returned (or otherwise used outside) so that enclosing scope would normally be gone. Python detects this and keeps those referenced variables alive in cell objects attached to the inner function."
}
},
{
"@type": "Question",
"name": "When should I use a closure instead of a class in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use a closure when you need a single callable with a small amount of private configuration or state — function factories and simple counters are ideal. Switch to a class when you need multiple methods, more complex state management, inheritance, or when you want straightforward `instance.attribute` introspection. The rule of thumb: if you're returning more than two inner functions, a class will be cleaner."
}
},
{
"@type": "Question",
"name": "Does Python's garbage collector clean up closure variables if the closure is no longer referenced?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes. The free variables are kept alive by reference counting and the garbage collector as long as at least one closure that references them still exists. Once all closures referencing those cell objects are garbage-collected, the cell objects — and their contents — are freed too. There's no special memory leak risk unique to closures beyond ordinary reference cycles."
}
},
{
"@type": "Question",
"name": "How can I see which variables a closure captured at runtime?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use `func.__code__.co_freevars` to get a tuple of variable names, and `func.__closure__` to get a tuple of cell objects. Each cell has a `cell_contents` attribute that gives the current value. If `func.__closure__` is `None`, the function is not a closure."
}
},
{
"@type": "Question",
"name": "Why do closures inside a loop all return the same value?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Because closures capture variables by reference, not by value. All functions created in the loop share the same loop variable. By the time you call them, the loop has finished and the variable holds its final value. Fix with a default argument or a factory function."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large