Questions tagged [with-statement]

A number of languages have With statements. The Python with statement creates a new context, with associated context manager. When the context (a code block) is exited again, the context manager is notified. Please use "common-table-expression" for the SQL WITH construction.

A number of languages have With statements. Python's with statement creates a runtime context, defined by a . As the code block under the with statement is entered, a __enter__ hook is called on the context manager, and as it is exited (by any means, including exceptions and return statements), the __exit__ hook is called.

Python provides several standard context managers. File objects, for example, can be opened as a context manager, and on exit the file is automatically closed.

Context managers were defined in PEP 343.

Please use for the SQL WITH statement

1119 questions
36
votes
7 answers

Catching exception in context manager __enter__()

Is it possible to ensure the __exit__() method is called even if there is an exception in __enter__()? >>> class TstContx(object): ... def __enter__(self): ... raise Exception('Oops in __enter__') ... ... def __exit__(self, e_typ,…
tMC
  • 18,105
  • 14
  • 62
  • 98
35
votes
4 answers

Oracle SQL insert into with With clause

I'm new to sql, so maybe it is a dumb question, but is there any possibility to use With clause with Insert Into? Or are there any common workarounds? I mean something like this: With helper_table As ( Select * From dummy2 ) Insert Into dummy1…
user2424380
  • 1,393
  • 3
  • 16
  • 29
35
votes
14 answers

Is Delphi "with" keyword a bad practice?

I been reading bad things about the with keyword in delphi but, in my opinion, if you don't over use it. It can make your code look simple. I often put all my TClientDataSets and TFields in TDataModules. So in my forms I had code like this procedure…
Marioh
  • 892
  • 3
  • 8
  • 14
34
votes
3 answers

Are multiple `with` statements on one line equivalent to nested `with` statements, in python?

Are these two statements equivalent? with A() as a, B() as b: # do something with A() as a: with B() as b: # do something I ask because both a and b alter global variables (tensorflow here) and b depends on changes made by a. So I know the…
David Parks
  • 30,789
  • 47
  • 185
  • 328
34
votes
4 answers

Python nested context manager on multiple lines

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,…
Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134
33
votes
1 answer

Return value of __exit__

I understand that __enter__ and __exit__ are used to implement a context manager. if an exception occurs in a with statement, the exception's type, value and traceback are passed to the __exit__ method. __exit__ can handle the…
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
32
votes
4 answers

multiprocessing returns "too many open files" but using `with...as` fixes it. Why?

I was using this answer in order to run parallel commands with multiprocessing in Python on a Linux box. My code did something like: import multiprocessing import logging def cycle(offset): # Do stuff def run(): for nprocess in…
nephewtom
  • 2,941
  • 3
  • 35
  • 49
32
votes
2 answers

python 'with' statement, should I use contextlib.closing?

This is from flask tutorial Step 3: from contextlib import closing def init_db(): with closing(connect_db()) as db: with app.open_resource('schema.sql') as f: db.cursor().executescript(f.read()) …
su79eu7k
  • 7,031
  • 3
  • 34
  • 40
31
votes
1 answer

Is Python *with* statement exactly equivalent to a try - (except) - finally block?

I know this was widely discussed, but I still can't find an answer to confirm this: is the with statement identical to calling the same code in a try - (except) -finally block, where whatever one defines in the __exit__ function of the context…
Clara
  • 2,935
  • 6
  • 34
  • 49
31
votes
4 answers

In R, how to make the variables inside a function available to the lower level function inside this function?(with, attach, environment)

Update 2 @G. Grothendieck posted two approaches. The second one is changing the function environment inside a function. This solves my problem of too many coding replicates. I am not sure if this is a good method to pass through the CRAN check when…
Zhenglei
  • 988
  • 1
  • 10
  • 21
30
votes
3 answers

Object becomes None when using a context manager

Why doesn`t this work: class X: var1 = 1 def __enter__(self): pass def __exit__(self, type, value, traceback): pass with X() as z: print z.var1 I get: print z.var1 AttributeError: 'NoneType' object has no attribute 'var1'
Weholt
  • 1,889
  • 5
  • 22
  • 35
30
votes
6 answers

Nesting 'WITH' statements in Python

It turns out that "with" is a funny word to search for on the internet. Does anyone knows what the deal is with nesting with statements in python? I've been tracking down a very slippery bug in a script I've been writing and I suspect that it's…
Zefira
  • 4,329
  • 3
  • 25
  • 31
29
votes
6 answers

Equivalence of "With...End With" in C#?

I know that C# has the using keyword, but using disposes of the object automatically. Is there the equivalence of With...End With in Visual Basic 6.0?
odiseh
  • 25,407
  • 33
  • 108
  • 151
27
votes
6 answers

Understanding the Python with statement and context managers

I am trying to understand the with statement. I understand that it is supposed to replace the try/except block. Now suppose I do something like this: try: name = "rubicon" / 2 # to raise an exception except Exception as e: print("No, not…
user444997
  • 273
  • 1
  • 3
  • 4
27
votes
8 answers

Python context manager that measures time

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): …
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
1 2
3
74 75