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/
Domain: thecodeforge.io
{
"@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: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