Title: Abstract Base Classes in Python: Python ABCs — Catch Missing
Open Graph Title: Abstract Base Classes in Python: Python ABCs — Catch Missing
Description: A missing charge() returned None for 3 days—costly losses. ABCs raise TypeError at instantiation time, not after silent failures hit your revenue.
Open Graph Description: A missing charge() returned None for 3 days—costly losses. ABCs raise TypeError at instantiation time, not after silent failures hit your revenue.
Opengraph URL: https://thecodeforge.io/python/abstract-base-classes-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/abstract-base-classes-python/"
},
"headline": "Python ABCs — Catch Missing Methods at Instantiation",
"description": "A missing charge() returned None for 3 days—costly losses. ABCs raise TypeError at instantiation time, not after silent failures hit your revenue.",
"keywords": "Python ABCs — Catch Missing Methods at Instantiation, Python, Intermediate, Advanced Python, A missing charge() returned None for 3 days—costly losses. ABCs raise TypeError at instantiation time, not after silent failures hit your revenue.",
"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-06T00:00:00+00:00",
"dateModified": "2026-10-31T00:00:00+00:00",
"url": "https://thecodeforge.io/python/abstract-base-classes-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 ABCs — Catch Missing Methods at Instantiation", "item": "https://thecodeforge.io/python/abstract-base-classes-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Does Python have interfaces like Java or TypeScript?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Python does not have a formal interface keyword, but Abstract Base Classes serve the same structural purpose. An ABC where every method is decorated with @abstractmethod and no concrete implementation is provided is functionally identical to a Java interface or a TypeScript interface — it defines a contract that implementing classes must satisfy. Python also offers typing.Protocol for structural subtyping (checking that an object has the right methods without requiring inheritance), which is closer to TypeScript's structural type system. For enforcement at instantiation time, ABCs are the standard mechanism."
}
},
{
"@type": "Question",
"name": "Can you instantiate an Abstract Base Class directly?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No — and that is the entire point. If you try to call MyABC() where MyABC has any unimplemented abstract methods, Python raises TypeError immediately: \"Can't instantiate abstract class MyABC without an implementation for abstract method 'method_name'\". The ABC is a contract, not a usable object. Only concrete subclasses that implement every abstract method can be instantiated."
}
},
{
"@type": "Question",
"name": "What is the difference between @abstractmethod and raising NotImplementedError in the base class?",
"acceptedAnswer": {
"@type": "Answer",
"text": "They catch the problem at different times. NotImplementedError is raised when the unimplemented method is called at runtime — which could be days or weeks after the broken code was deployed. If the method is never called in your test suite, the bug is never discovered. @abstractmethod on an ABC is enforced at instantiation time — Python refuses to create the object at all if the method is missing. The bug is caught during development, during import, during CI — never in production. ABCs also produce a clearer error message that names the exact missing method and the exact class."
}
},
{
"@type": "Question",
"name": "When should I use typing.Protocol instead of ABC?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use Protocol when you want structural subtyping — checking that an object has the right methods based on its structure, without requiring it to inherit from a specific base class. Protocol works with static type checkers like mypy and does not enforce anything at runtime. Use ABC when you want runtime enforcement at instantiation time — a TypeError that fires before any code runs. The practical guidance: Protocol is for static analysis and type checking during development. ABC is for runtime enforcement in production. They are complementary — you can use both in the same codebase for different purposes."
}
}
]
}
| 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