René's URL Explorer Experiment


Title: Classes and Objects in Python: Python __init__ Mutable

Open Graph Title: Classes and Objects in Python: Python __init__ Mutable

Description: Default list in __init__ caused cross-user data leaks in production. How Python evaluates mutable defaults once at definition, and the None sentinel fix.

Open Graph Description: Default list in __init__ caused cross-user data leaks in production. How Python evaluates mutable defaults once at definition, and the None sentinel fix.

Opengraph URL: https://thecodeforge.io/python/classes-objects-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/classes-objects-python/"
  },
  "headline": "Python __init__ Mutable Defaults — The Shared State Bug",
  "description": "Default list in __init__ caused cross-user data leaks in production. How Python evaluates mutable defaults once at definition, and the None sentinel fix.",
  "keywords": "Python __init__ Mutable Defaults — The Shared State Bug, Python, Intermediate, OOP in Python, Default list in __init__ caused cross-user data leaks in production. How Python evaluates mutable defaults once at definition, and the None sentinel fix.",
  "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-26T00:00:00+00:00",
  "url": "https://thecodeforge.io/python/classes-objects-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 __init__ Mutable Defaults — The Shared State Bug", "item": "https://thecodeforge.io/python/classes-objects-python/"}
  ]
}
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [
    {
      "@type": "Question",
      "name": "What is the difference between a class and an object in Python?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "A class is the blueprint — it defines the structure and behaviour but holds no data itself. An object is a live instance created from that blueprint, with its own copy of the instance attributes. You can create thousands of independent objects from one class, just like stamping cookies from one cutter."
      }
    },
    {
      "@type": "Question",
      "name": "Why does Python use 'self' in class methods?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "When you call a method on an instance, Python automatically passes that instance as the first argument. 'self' is just the conventional name for that parameter — it's how the method knows which object's data to read or change. Technically you can name it anything, but the convention is universal and you should always follow it."
      }
    },
    {
      "@type": "Question",
      "name": "When should I use a class instead of a plain dictionary or function in Python?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "Use a class when you have both data AND behaviour that belong together and will be used repeatedly. A plain dictionary is fine for passive data bags. A function is fine for a single transformation. But if you find yourself writing functions that all take the same dictionary as their first argument, that's a strong signal to reach for a class instead."
      }
    },
    {
      "@type": "Question",
      "name": "What is the purpose of __repr__ vs __str__?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "__repr__ should be an unambiguous representation of the object, ideally valid Python code that could recreate it (e.g., `Vector(3, 4)`). It's used by the REPL, logging, and debugging tools. __str__ should be a human-readable representation (e.g., `(3, 4)`). It's used by `print()` and `str()`. Python falls back to __repr__ if __str__ is missing, but not the other way around. Always implement at least __repr__ for every class you use in production."
      }
    },
    {
      "@type": "Question",
      "name": "How does Python's name mangling work with double underscores?",
      "acceptedAnswer": {
        "@type": "Answer",
        "text": "When you write `self.__attribute`, Python automatically rewrites it to `self._ClassName__attribute`. This prevents accidental overrides in subclasses. For example, a parent class with `self.__private` and a child that also defines `self.__private` will have distinct attributes: `_Parent__private` and `_Child__private`. It's not true privacy (you can still access the mangled name), but it avoids name collisions. Use double underscores only when you specifically need to prevent subclass interference; use single underscore for most internal attributes."
      }
    }
  ]
}

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
1Classes and Objects in Pythonhttps://thecodeforge.io/python/classes-objects-python/
2Inheritance in Pythonhttps://thecodeforge.io/python/inheritance-python/
3Polymorphism in Pythonhttps://thecodeforge.io/python/polymorphism-python/
4Encapsulation in Pythonhttps://thecodeforge.io/python/encapsulation-python/
5Magic Methods in Pythonhttps://thecodeforge.io/python/magic-methods-python/
6Abstract Classes in Pythonhttps://thecodeforge.io/python/abstract-classes-python/
7Multiple Inheritance in Pythonhttps://thecodeforge.io/python/multiple-inheritance-python/
8dataclasses in Pythonhttps://thecodeforge.io/python/dataclasses-python/
9Property Decorators in Pythonhttps://thecodeforge.io/python/property-decorators-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/classes-objects-python/#v2-production-incident
 🔎 Debug Guidehttps://thecodeforge.io/python/classes-objects-python/#v2-debug-guide
 ⚙ Triage Commandshttps://thecodeforge.io/python/classes-objects-python/#v2-triage-commands
Pythonhttps://thecodeforge.io/python/introduction-python/
Djangohttps://thecodeforge.io/interview/django-interview-questions/
machinehttps://thecodeforge.io/ml-ai/introduction-machine-learning/
functionshttps://thecodeforge.io/python/functions-python/
workinghttps://thecodeforge.io/python/json-python/
inheritancehttps://thecodeforge.io/python/inheritance-python/
metaclasseshttps://thecodeforge.io/python/metaclasses-python/
→Python Dataclasses — Mutable Default Traps That Break ProdShared list defaults corrupted customer orders across instances. Learn why @dataclass protection fails when you overridehttps://thecodeforge.io/python/dataclasses-python/
→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 Magic Methods — Set Corruption When __hash__ MissingOrders missing from set when __eq__ defined without __hash__ triggers TypeError and silent corruption. Use frozen dataclhttps://thecodeforge.io/python/magic-methods-python/
→Python OOP MRO Failure — Fix super().__init__ in MixinsAttributeError on a parent attribute? In cooperative inheritance, a missing super().__init__ in a mixin breaks the MRO chttps://thecodeforge.io/interview/python-oop-interview-questions/
PyTorch Tensors ExplainedBeginnerhttps://thecodeforge.io/ml-ai/pytorch-tensors/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←Previousfunctools Module in Pythonhttps://thecodeforge.io/python/functools-module-python/
NextInheritance in Python→https://thecodeforge.io/python/inheritance-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.