René's URL Explorer Experiment


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/

direct link

Domain: thecodeforge.io


Hey, it has json ld scripts:
{
  "@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:typewebsite
og:imagehttps://thecodeforge.io/assets/og-image.png
og:image:width1200
og:image:height630

Links:

TheCodeForge https://thecodeforge.io/
Pythonhttps://thecodeforge.io/python/
Buy me a coffee https://ko-fi.com/thecodeforge
1Functions in Pythonhttps://thecodeforge.io/python/functions-python/
2*args and **kwargs in Pythonhttps://thecodeforge.io/python/args-kwargs-python/
3Lambda Functions in Pythonhttps://thecodeforge.io/python/lambda-functions-python/
4Decorators in Pythonhttps://thecodeforge.io/python/decorators-python/
5Generators in Pythonhttps://thecodeforge.io/python/generators-python/
6Closures in Pythonhttps://thecodeforge.io/python/closures-python/
7Recursion in Pythonhttps://thecodeforge.io/python/recursion-python/
8map filter and reduce in Pythonhttps://thecodeforge.io/python/map-filter-reduce-python/
9Built-in Functions in Pythonhttps://thecodeforge.io/python/built-in-functions-python/
10Higher Order Functions in Pythonhttps://thecodeforge.io/python/higher-order-functions-python/
11functools Module in Pythonhttps://thecodeforge.io/python/functools-module-python/
← Back to Python track https://thecodeforge.io/python/
Homehttps://thecodeforge.io/
Pythonhttps://thecodeforge.io/python/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
 ● Production Incidenthttps://thecodeforge.io/python/closures-python/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/python/closures-python/#v2-debug-guide
 ⚙ Triage Commandshttps://thecodeforge.io/python/closures-python/#v2-triage-commands
Pythonhttps://thecodeforge.io/python/introduction-python/
functionshttps://thecodeforge.io/python/functions-python/
decoratorshttps://thecodeforge.io/python/decorators-python/
This keywordhttps://thecodeforge.io/javascript/this-keyword-javascript/
factoryhttps://thecodeforge.io/java/factory-pattern-java/
decoratorhttps://thecodeforge.io/java/decorator-pattern-java/
generatorshttps://thecodeforge.io/python/generators-python/
Inheritancehttps://thecodeforge.io/python/inheritance-python/
serializationhttps://thecodeforge.io/java/serialization-java/
containerhttps://thecodeforge.io/dsa/container-with-most-water/
encapsulationhttps://thecodeforge.io/python/encapsulation-python/
designhttps://thecodeforge.io/java/design-patterns-java/
→JavaScript Closures — Loop Traps and Memory Leak PatternsEvent listener closures capturing component state cause monotonic memory growth. Find retained DOM nodes with heap snapshttps://thecodeforge.io/javascript/closures-javascript/
→JavaScript Closures — Silent Memory Leak from DOM NodesRSS memory grew for 6h until OOMKill — closures captured jsdom DOM nodes. Heap snapshots show detached DOM trees. Real ihttps://thecodeforge.io/interview/javascript-closures-interview-questions/
→Python Mutable Default Arguments — Data-Leak GotchaIntermittent personal data leaks? Python's mutable default args cause shared state at definition - avoid this gotcha thahttps://thecodeforge.io/interview/top-python-interview-questions/
→Python Lambda Late Binding Bug Charged 500 WrongPython lambda functions explained: one-line anonymous functions for sort keys, map and filter. When to use a lambda, whehttps://thecodeforge.io/python/lambda-functions-python/
→functools @lru_cache — Cross-User Cache LeakA @lru_cache on an instance method shared cache across users via identical self — debugging steps and per-instance cachehttps://thecodeforge.io/python/functools-module-python/
How to Deploy Your First ML Model with Flask or FastAPI (Beginner)Beginnerhttps://thecodeforge.io/ml-ai/deploy-first-ml-model-flask-fastapi/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←PreviousGenerators in Pythonhttps://thecodeforge.io/python/generators-python/
NextRecursion in Python→https://thecodeforge.io/python/recursion-python/
LinkedIn https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
Facebook https://www.facebook.com/profile.php?id=61583020033815
YouTube https://www.youtube.com/channel/UCMar7CF2EmCF1I2tw1tNVuQ
Javahttps://thecodeforge.io/java/
Pythonhttps://thecodeforge.io/python/
C / C++https://thecodeforge.io/c-cpp/
JavaScripthttps://thecodeforge.io/javascript/
DSAhttps://thecodeforge.io/dsa/
Databasehttps://thecodeforge.io/database/
System Designhttps://thecodeforge.io/system-design/
DevOpshttps://thecodeforge.io/devops/
C# / .NEThttps://thecodeforge.io/csharp/
PHPhttps://thecodeforge.io/php/
ML / AIhttps://thecodeforge.io/ml-ai/
Interviewhttps://thecodeforge.io/interview/
CS Fundamentalshttps://thecodeforge.io/cs-fundamentals/
Rusthttps://thecodeforge.io/rust/
Toolshttps://thecodeforge.io/tools/
Cheatsheetshttps://thecodeforge.io/cheatsheets/
Mobilehttps://thecodeforge.io/mobile/
Data Analyticshttps://thecodeforge.io/data-analytics/
Securityhttps://thecodeforge.io/security/
Frontendhttps://thecodeforge.io/frontend/
Data Engineeringhttps://thecodeforge.io/data-engineering/
Testinghttps://thecodeforge.io/testing/
Cloudhttps://thecodeforge.io/cloud/
Observabilityhttps://thecodeforge.io/observability/
Web Platformhttps://thecodeforge.io/web-platform/
🧯 Errors Explainedhttps://thecodeforge.io/errors/
⚒ Blueprintshttps://thecodeforge.io/blueprints/
⌨ Practicehttps://thecodeforge.io/practice/
Interview Prephttps://thecodeforge.io/interview/
DSA Problemshttps://thecodeforge.io/dsa/
Developer Toolshttps://thecodeforge.io/tools/
Cheat Sheetshttps://thecodeforge.io/cheatsheets/
Abouthttps://thecodeforge.io/about/
Sitemaphttps://thecodeforge.io/sitemap.xml
Privacyhttps://thecodeforge.io/privacy.html

Viewport: width=device-width, initial-scale=1.0

Robots: max-image-preview:large


URLs of crawlers that visited me.