Proposal: Use decorator instead of get_classes to mark classes extensible
See original GitHub issueWhile struggling with the pain that is get_classes
statements spanning across multiple lines, I wished for a cleaner way to handle Oscar’s dynamic class loading
Proposal
Introduce a class decorator that mimics get_class
. If a customised version of the decorated class exists, return that one instead of the decorated class.
# catalogue/forms.py
from oscar.core.loading import extensible
class ProductForm(forms.Form):
...
@extensible
class ExtensibleProductForm(ProductForm):
pass
# catalogue/views.py
from .forms import ExtensibleProductForm
Pro & Con
Pro:
- more readable import statements
- IDE support for importing, and detecting unused or wrong imports
- avoids accidentally using a direct import, class will always be extensible
- a lot DRYer than the current solution
Cons:
- It’s not really “decorating”, it’s a straight-out monkey patch.
- Makes class names longer
One could also just decorate all extensible classes, but then it’s not immediately obvious from other code if the used class is extensible or not. And it makes it harder to access the core Oscar class.
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Decorator pattern versus sub classing - Stack Overflow
The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original...
Read more >5.3-Decorator Design Pattern - GitHub Pages
ConcreteComponent is a representative for all classes whose objects should be dynamically extensible with new functionality. Component can be: an interface that ...
Read more >This Is How You Extend Your Classes Without Modifying Them
Plain and simple extension over modification using decorators. Ever had new requirements thrown at you where you'd need to add new functionality ...
Read more >Head First Design Patterns
“'Head First Design Patterns' manages to mix fun, belly-laughs, insight, technical depth and great practical advice in one entertaining and thought provoking ...
Read more >Annotations vs Decorators · Issue #115 - GitHub
Java Annotations are just that: annotations. A kind of comment. It is a kind of metadata that expresses the properties of classes, attributes, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree that the current approach is more explicit and therefore better than the proposal.
If it were to change I’d rather it went the other way and became more explicit, i.e., as well as using
get_class
, using a registry pattern rather than using magic paths.Closing this on the basis that the consensus seems to be to make overriding more explicit, not less. I think the solution will be related to #2544.