Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python closing context manager

The standard library open function works both as a function:

f = open('file.txt')
print(type(f))
<type 'file'>

or as a context manager:

with open('file.txt') as f:
    print(type(f))
<type 'file'>

I am trying to mimic this behaviour using contextlib.closing, where File is my custom file I/O class:

def myopen(filename):
    f = File(filename)
    f.open()
    return closing(f)

this works as expected as a context manager:

with myopen('file.txt') as f:
    print(type(f))
<class '__main__.File'>

but of course if I call directly, I get back the closing object instead of my object:

f = myopen(filename)
print(type(f))
<class 'contextlib.closing'>

So, how do I implement myopen so that it both works as a context manager and returns my File object when called directly?

Full working example on github: https://gist.github.com/1352573

like image 627
Andrea Zonca Avatar asked Nov 09 '11 19:11

Andrea Zonca


1 Answers

The easiest thing is probably to implement the __enter__ and __exit__ methods yourself. Something like this should do it:

class File(object):
   # ... all the methods you already have ...

   # context management
   def __enter__(self):
       return self
   def __exit__(self, *exc_info):
       self.close()

It would, by the way, be more idiomatic to do the work of your open method in your __init__ method.

like image 100
zwol Avatar answered Sep 22 '22 19:09

zwol