I'm trying to use the with statement with a class I implemented. But I don't want the constructor of the class to be called.
I tried this:
class Model(ABC):
def __init__(self):
# some code that uses class variable from child classes
# should not be called with with syntax
pass
@classmethod
def __enter__(cls):
print('call enter')
return cls
@classmethod
def __exit__(cls, *args):
print('call exit')
with Model:
print('inside with')
but i get the error: AttributeError: __enter__
I don't understand why i get this error (my class has an enter method). Is there a way to make it work ?