with-statement

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__ Question: I have searched and I’m unable to come up with any good reason to use python’s __enter__ /__exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, …

Total answers: 3

Python context manager that measures time

Python context manager that measures time Question: I am struggling to make a piece of code that allows to measure time spent within a “with” statement and assigns the time measured (a float) to the variable provided in the “with” statement. import time class catchtime: def __enter__(self): self.t = time.clock() return 1 def __exit__(self, type, …

Total answers: 8

Python nested context manager on multiple lines

Python nested context manager on multiple lines Question: In Python 2.6, we used to format our nested context manager that way: with nested( context1, context2 ) as a, b: pass From Python 2.7 and on, nested is deprecated. I’ve seen lots of example of multiple context manager on a single line, but I can’t find …

Total answers: 4

Can I use pymysql.connect() with "with" statement?

Can I use pymysql.connect() with "with" statement? Question: The following is listed as example in pymysql: conn = pymysql.connect(…) with conn.cursor() as cursor: cursor.execute(…) … conn.close() Can I use the following instead, or will this leave a lingering connection? (it executes successfully) import pymysql with pymysql.connect(…) as cursor: cursor.execute(‘show tables’) (python 3, latest pymysql) Asked …

Total answers: 4

Python multi-line with statement

Python multi-line with statement Question: What is a clean way to create a multi-line with in python? I want to open up several files inside a single with, but it’s far enough to the right that I want it on multiple lines. Like this: class Dummy: def __enter__(self): pass def __exit__(self, type, value, traceback): pass …

Total answers: 6

How to check if an object is created with `with` statement?

How to check if an object is created with `with` statement? Question: I would like to ensure that the class is only instantiated within a “with” statement. i.e. this one is ok: with X() as x: … and this is not: x = X() How can I ensure such functionality? Asked By: vpas || Source …

Total answers: 6

Conditional with statement in Python

Conditional with statement in Python Question: Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify, one scenario would have a block …

Total answers: 9

How to __enter__ n context managers?

How to __enter__ n context managers? Question: Using the with statement, we can enter many context handlers using only one level of indentation/nesting: >>> from contextlib import contextmanager >>> @contextmanager … def frobnicate(n): … print(‘frobbing {}’.format(n)) … yield … >>> frob1 = frobnicate(1) >>> frob2 = frobnicate(2) >>> with frob1, frob2: … pass … frobbing …

Total answers: 1

Meaning of "with" statement without "as" keyword

Meaning of "with" statement without "as" keyword Question: I’m familiar with using python’s with statement as a means of ensuring finalization of an object in the event of an exception being thrown. This usually looks like with file.open(‘myfile.txt’) as f: do stuff… which is short-hand for f = file.open(‘myfile.txt’): try: do stuff… finally: f.close() or …

Total answers: 2