Levi
Levi

Reputation: 77

Python context manager

Trying to use my little method but I get follow error:

class mycls:

    def __init__(self):
        ...

    def __enter__(self):
        ...

    def little(self):
        ...

    def __exit__(self, exc_type, exc_val, exc_tb):
        ...


with mycls() as cl:
    cl.little()
    with cl:
        cl.little()
        with cl:
            cl.little()
    cl.little()

Error:

AttributeError: 'NoneType' object has no attribute 'little'

Upvotes: 2

Views: 54

Answers (2)

Ajax1234
Ajax1234

Reputation: 71471

You need to return self from __enter__:

class mycls:
   def __enter__(self):
      return self
   def __exit__(self, *_):
      pass
   def little(self):
      pass

with mycls() as cl:
   cl.little()
   with cl:
      cl.little()
      with cl:
         cl.little()
   cl.little()

Upvotes: 3

chepner
chepner

Reputation: 532528

The with statement does not bind the instance of mycls itself to cl, but rather the return value of that instance's __enter__ method. Currently, mycls.__enter__ returns None, hence the observed error. Change __enter__ to

def __enter__(self):
    return self

and your code should work as expected.

Code like

with foo as bar:
    ...

is (ignoring a lot of details) roughly the same as

x = foo()
bar = x.__enter__()
...
x.__exit__()

Upvotes: 4

Related Questions