A decorator for flexible inheritance in Python
Project description
super_init - A Flexible Inheritance Decorator for Python
Overview
super_init is a Python decorator that provides more control over parent class initialization when using inheritance. It allows developers to selectively invoke the parent class's __init__ method using a simple flag, rather than always requiring explicit super().__init__() calls.
This decorator is particularly useful for:
- Preventing unintended calls to parent initializers.
- Making class hierarchies more flexible.
- Reducing boilerplate code in OOP-based Python applications.
Installation
You can install super_init via pip:
pip install super_init
How It Works
By default, Python requires manually calling super().__init__() inside a subclass if you want the parent’s __init__ to execute. The @super_init decorator changes this by adding an optional _use_super parameter to the subclass’s constructor.
Without @super_init (Standard Python Inheritance)
class Parent:
def __init__(self):
print("Parent __init__ called")
class Child(Parent):
def __init__(self):
print("Child __init__ called")
super().__init__()
c = Child()
# Output:
# Child __init__ called
# Parent __init__ called
In this approach, the parent class’s __init__ is always executed when super().__init__() is explicitly called.
With @super_init Decorator
from super_init import super_init
class Parent:
def __init__(self):
print("Parent __init__ called")
@super_init
class Child(Parent):
def __init__(self):
print("Child __init__ called")
c1 = Child()
# Output:
# Child __init__ called
c2 = Child(_use_super=True)
# Output:
# Child __init__ called
# Parent __init__ called
Now, the parent class __init__ is only called if explicitly requested by passing _use_super=True.
Use Cases
1. Simplifying OOP in Python
Many real-world applications involve complex inheritance structures where not every subclass needs to call the parent’s __init__. @super_init makes this decision explicit at runtime.
@super_init
class AdvancedChild(Parent):
def __init__(self, data):
print(f"AdvancedChild initialized with data: {data}")
if data:
super().__init__()
2. Django Models Example
Django models often require calling the parent’s initializer when overriding the default behavior.
from django.db import models
from super_init import super_init
@super_init
class BaseModel(models.Model):
class Meta:
abstract = True
def __init__(self, *args, **kwargs):
print("BaseModel __init__ called")
super().__init__(*args, **kwargs)
@super_init
class MyModel(BaseModel):
name = models.CharField(max_length=100)
def __init__(self, *args, **kwargs):
print("MyModel __init__ called")
super().__init__(*args, **kwargs)
# Usage
obj = MyModel(_use_super=True)
# Output:
# MyModel __init__ called
# BaseModel __init__ called
3. Third-Party Libraries
You can use @super_init in frameworks like Flask, FastAPI, or Pydantic to control when base class initialization occurs dynamically.
Existing Limitations of Python’s OOP
1. Unnecessary Parent Initialization
In standard Python OOP, calling super().__init__() is required for running parent initialization, even if it's sometimes unnecessary. This leads to redundant calls and extra processing.
2. Hidden Bugs in Multi-Inheritance
In multiple inheritance scenarios, forgetting to call super().__init__() in some subclasses can cause unexpected behavior.
3. Boilerplate Code
Developers often write unnecessary super().__init__() calls, making the code verbose.
What super_init Changes
✅ Selective Parent Initialization
With @super_init, subclasses only invoke the parent’s __init__ when required, reducing redundant execution.
✅ Cleaner Code
You don’t have to manually check whether to call super().__init__() in every subclass. The _use_super flag makes it explicit at object creation.
✅ Supports Existing Python Code
@super_init does not interfere with standard inheritance but enhances its flexibility.
Running Tests
To verify functionality, run the tests:
pytest tests/
Contributing
We welcome contributions! If you have ideas to improve super_init, feel free to submit a pull request.
- Fork the repository.
- Clone the fork:
git clone https://github.com/your-username/super-init.git
- Create a new branch:
git checkout -b feature-branch
- Make changes and run tests.
- Push and create a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Developed by Abhijeet Kumar.
Links
- GitHub Repository: https://github.com/csabhijeet/super-init
- PyPI Package: https://pypi.org/project/super-init/ (Coming soon!)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file super_init-0.1.0.tar.gz.
File metadata
- Download URL: super_init-0.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8795c9f42b8a212fbdde8dd87049eddfcbb349a4fccd159c6d88f14dae351fb7
|
|
| MD5 |
5f7db5c680443e8e04e29f03cf26bbf9
|
|
| BLAKE2b-256 |
ded2e8edb4007f636e7e8b274beeb9e47243476cc613856506a3ea7fef881bfe
|
File details
Details for the file super_init-0.1.0-py3-none-any.whl.
File metadata
- Download URL: super_init-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63b26de358d96ad53fb394e3f8bd7d5be858a3ccba488a8b4fd351661f6ab4b5
|
|
| MD5 |
220e99575951b5275c59f09f3c6057ac
|
|
| BLAKE2b-256 |
e39bb27c52ad320c4733ea0e141be85f49f1b86504482266152954f1399ca1c5
|