简体   繁体   中英

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'

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__()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM