Imagine I have some code that I want it to run:
with F() as o:
while True:
a = o.send(2)
print(a)
It means that the F
class should return an generator
and also it is context manager
, generally I want a context manager to be generator too.
I tried this:
class F:
def __enter__(self):
return self
def __exit__(self, *exc):
print('exit')
def __next__(self):
return 5
def __iter__(self):
return self
As expected this will return AttributeError: 'F' object has no attribute 'send'
, I handled this error by adding:
def send(self, param):
self.__next__()
but I think it is not a good way to do this, I look around and find this, but they are not using send
as I want, I need that instance to be a generator.