with-statement

Are there legitimate uses for JavaScript's "with" statement?

Alan Storm's comments in response to my answer regarding the with statement got me thinking. I've seldom found a reason to use this particular language feature, and had never given much thought to how it might cause trouble. Now, I'm curious as to how I might make effective use of with, while avoiding its pitfalls... So my question is, ...

What's wrong with Delphi's "with"

I've heard many programmers, particularly Delphi programmers scorn the use of 'with'. I thought it made programs run faster (only one reference to parent object) and that it was easier to read the code if used sensibly (less than a dozen lines of code and no nesting). Here's an example: procedure TBitmap32.FillRectS(const ARect: TRect...

The VB.NET 'With' Statement - embrace or avoid?

At work, I'm frequently working on projects where numerous properties of certain objects have to be set during their construction or early during their lifetime. For the sake of convenience and readability, I often use the With statement to set these properties. I find that With Me.Elements .PropertyA = True .PropertyB = "Inacti...

Why is the with() construct not included in C#, when it is really cool in VB.NET?

I am C# developer. I really love the curly brace because I came from C, C++ and Java background. However, I also like the other programming languages of the .NET Family such as VB.NET. Switching back and forth between C# and VB.NET is not really that big of deal if you have been programming for a while in .NET. That is very common appro...

Using "with" statement for CSV files in Python

Is it possible to use the with statement directly with CSV files? It seems natural to be able to do something like this: import csv with csv.reader(open("myfile.csv")) as reader: # do things with reader But csv.reader doesn't provide the __enter__ and __exit__ methods, so this doesn't work. I can however do it in two steps: impor...

problem using an instance in a with_statement

I've recently started to learn python , and I reached the with statement . I've tried to use it with a class instance , but I think I'm doing something wrong . Here is the code : from __future__ import with_statement import pdb class Geo: def __init__(self,text): self.text = text def __enter__(self): print "entering" ...

Reference object instance created using "with" in Delphi

Hi, is there a way to reference an object instance that is created using the "with" statement? Example: with TAnObject.Create do begin DoSomething(instance); end; Where DoSomething would use the instance reference as if you were passing an instance from a variable declared reference to the object created. Example: AnObject := TAn...

How should I return interesting values from a with-statement?

Is there a better way than using globals to get interesting values from a context manager? @contextmanager def transaction(): global successCount global errorCount try: yield except: storage.store.rollback() errorCount += 1 else: storage.store.commit() successCount += 1 Other...

Multiple variables in Python 'with' statement

Is it possible to declare more than one variable using a with statement in Python? Something like: from __future__ import with_statement with open("out.txt","wt"), open("in.txt") as file_out, file_in: for line in file_in: file_out.write(line) ... or is cleaning up two resources at the same time the problem? ...

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

Hi I know that c# has using element....but as you know, using disposes object automatically... clearly, I want the equivalence of with......end with in vb6.0? Merci ...

Delphi 2009 Handling of With

Anybody know what is different about Delphi 2009's handling of "with"? I fixed a problem yesterday just by deconstructing "with" to full references, as in "with Datamodule, Dataset, MainForm". Delphi 2006 and earlier applied "Close" to the Dataset. Delphi 2009 applied "Close" to the MainForm and exited the application! ...

How to access the object itself in With ... End With

Some code to illustrate my question: With Test.AnObject .Something = 1337 .AnotherThing = "Hello" ''// why can't I do this to pass the object itself: Test2.Subroutine(.) ''// ... and is there an equivalent, other than repeating the object in With? End With ...

Delphi: Since when are interface references no longer released at the end of a with-block?

I recently stumbled over a problem caused by some very old code I wrote which was obviously assuming that interface references used in a with statement would be released as soon as the with-block is left - kind of like an implicit try-finally-block (similar to C#'s using-statement if I understood correctly). Apparently (in Delphi 2009) ...

Finding Functions Defined in a with: Block

Here's some code from Richard Jones' Blog: with gui.vertical: text = gui.label('hello!') items = gui.selection(['one', 'two', 'three']) with gui.button('click me!'): def on_click(): text.value = items.value text.foreground = red My question is: how the heck did he do this? How can the conte...

How do I mock an open used in a with statement (using the Mock framework in Python)?

How do I test the following code with mocks (using mocks, the patch decorator and sentinels provided by Michael Foord's Mock framework): def testme(filepath): with open(filepath, 'r') as f: return f.read() ...

What is the WITH statement doing in this example? I'm trying to randomly generate data.

INSERT INTO files (fileUID, filename) WITH fileUIDS(fileUID) AS ( VALUES(1) UNION ALL SELECT fileUID+1 FROM fileUIDS WHERE fileUID < 1000 ) SELECT fileUID, TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdefgHij', '1234567890' ) FROM fileUIDS; ...

Can you create nested WITH clauses for Common Table Expressions?

WITH y AS ( WITH x AS ( SELECT * FROM MyTable ) SELECT * FROM x ) SELECT * FROM y Does something like this work? I tried it earlier but I couldn't get it to work. ...

Improvizing a drop-in replacement for the "with" statement for Python 2.4

Can you suggest a way to code a drop-in replacement for the "with" statement that will work in Python 2.4? It would be a hack, but it would allow me to port my project to Python 2.4 more nicely. EDIT: Removed irrelevant metaclass sketch ...

Any examples of a non-trivial and useful example of the 'with' keyword?

I still find the with keyword a bit...enigmatic. Briefly, with behaves like this: with (obj) { // do stuff } This adds obj to the head of the scope chain and then executes the with-block. When the block is finished it removes obj from the head of the scope chain. According to MDC, this allows you to do stuff like var a, x; ...

How can I conditionally execute code in a "with" block?

I guess this is kinda abusing the feature, but I'm still curious whether it could be done - I want to do something like: with conditional(a): print 1 so that the print 1 part is executed only if a==True. Is this possible? EDIT: Like people state below, this is horrible style. It's just a riddle\question. Don't try this at home, n...