0

The with statement does not appear to be returning the correct reference, instead it returns None.

class test():
    def __init__(self) -> None:
        pass
    def __enter__(self):
        print('entered')
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('exit')

with test() as t:
    print(type(t))
    print('inside')

Output:

>entered
><class 'NoneType'>
>inside
>exit

I would like t to be the reference to the test instance, not None.

Python V3.10

Ben K
  • 37
  • 6

1 Answers1

0
def __enter__(self):
    return self

The solution is to return a reference from __enter__

Ben K
  • 37
  • 6