I have a parent class that has a bunch of class methods:
class Parent():
@classmethod
def methodA(cls):
pass
@classmethod
def methodB(cls):
pass
In my subclass, I would like to wrap a subset of the methods inside a “with”. It should achieve this effect:
class Child(Parent):
@classmethod
def methodA(cls):
with db.transaction:
super(Child, cls).methodA()
I have a bunch of methods that follow this pattern and would prefer not to repeat myself. Is there a cleaner way to do this?
It seems you should move the with db.transaction into the base.
Make a method in the base, returning the db.transaction
then you overload it in the children as/if needed.
In the base you then do
Here’s a complete working example with a dummy transaction
this results in