129

I have to open a file-like object in python (it's a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the file in the constructor, and then closing it in the destructor. I'm getting weird errors though and I think it has to do with the garbage collector and such, I'm still not used to not knowing exactly when my objects are being deleted =\

The reason I was doing this is because I have to use tcsetattr with a bunch of parameters each time I open it and it gets annoying doing all that all over the place. So I want to implement an inner class to handle all that so I can use it doing
with Meter('/dev/ttyS2') as m:

I was looking online and I couldn't find a really good answer on how the with syntax is implemented. I saw that it uses the __enter__(self) and __exit(self)__ methods. But is all I have to do implement those methods and I can use the with syntax? Or is there more to it?

Is there either an example on how to do this or some documentation on how it's implemented on file objects already that I can look at?

Falmarri
  • 47,727
  • 41
  • 151
  • 191

3 Answers3

175

Those methods are pretty much all you need for making the object work with with statement.

In __enter__ you have to return the file object after opening it and setting it up.

In __exit__ you have to close the file object. The code for writing to it will be in the with statement body.

class Meter():
    def __init__(self, dev):
        self.dev = dev
    def __enter__(self):
        #ttysetattr etc goes here before opening and returning the file object
        self.fd = open(self.dev, MODE)
        return self
    def __exit__(self, type, value, traceback):
        #Exception handling here
        close(self.fd)

meter = Meter('dev/tty0')
with meter as m:
    #here you work with the file object.
    m.fd.read()
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
dekomote
  • 3,817
  • 1
  • 17
  • 13
71

Easiest may be to use standard Python library module contextlib:

import contextlib

@contextlib.contextmanager
def themeter(name):
    theobj = Meter(name)
    try:
        yield theobj
    finally:
        theobj.close()  # or whatever you need to do at exit


# usage
with themeter('/dev/ttyS2') as m:
    # do what you need with m
    m.read()

This doesn't make Meter itself a context manager (and therefore is non-invasive to that class), but rather "decorates" it (not in the sense of Python's "decorator syntax", but rather almost, but not quite, in the sense of the decorator design pattern;-) with a factory function themeter which is a context manager (which the contextlib.contextmanager decorator builds from the "single-yield" generator function you write) -- this makes it so much easier to separate the entering and exiting condition, avoids nesting, &c.

Majid
  • 3,128
  • 1
  • 26
  • 31
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 1
    This is much simpler than the class-based approach. – byxor Nov 28 '18 at 06:38
  • 14
    To ensure `theobj.close()` is executed even in case of an exception, you can wrap `yield theobj` with a `try...finally` block. – akesfeden Feb 26 '20 at 19:58
  • This should be the accepted answer. Not only is it simpler, but it's also cleaner to implement. I was also able to get chatgpt to refactor my code quickly to use this library, given that I had multiple embedded `with` statements. – autonopy Apr 25 '23 at 19:39
-15

The first Google hit (for me) explains it simply enough:

http://effbot.org/zone/python-with-statement.htm

and the PEP explains it more precisely (but also more verbosely):

http://www.python.org/dev/peps/pep-0343/

Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
  • 2
    I actually saw those and didn't think either of them were very clear at all. The first pretty much says "There are methods called \__enter__ and \__exit__ " and explains little to nothing about them. And the PEP pretty much just talks about the need for the new syntax – Falmarri Sep 22 '10 at 23:32
  • No, it's straightforward with examples and explanations. You need to read it again; this is not a complex feature. – Glenn Maynard Sep 22 '10 at 23:39
  • 20
    Regardless, you should know that link-only answers are poorly received by the community at large. At least share something from the source that supports your assertions. – IAbstract Aug 03 '19 at 10:59
  • 6
    first link is dead – Boris Verkhovskiy Jan 23 '21 at 08:37