Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

15
  • 56
    Technically speaking, one could call PackageResource().__enter__() explicitly and thus create a Package that would never be finalized... but they'd really have to be trying to break the code. Probably not something to worry about. Commented May 14, 2009 at 20:15
  • 4
    By the way, if you're using Python 2.5, you'll need to do from future import with_statement to be able to use the with statement. Commented May 14, 2009 at 20:39
  • 3
    I found an article which helps to show why __del__() acts the way it does and give credence to using a context manager solution: andy-pearce.com/blog/posts/2013/Apr/python-destructor-drawbacks Commented Jan 15, 2014 at 20:53
  • 2
    How to use that nice and clean construct if you want to pass parameters? I would like to be able to do with Resource(param1, param2) as r: # ... Commented Jan 20, 2014 at 9:52
  • 4
    @snooze92 you could give the Resource an __init__ method that stores *args and **kwargs in self, and then passes them on to the inner class in the enter method. When using the with statement, __init__ is called before __enter__ Commented Feb 6, 2014 at 17:32