with-statement

Does C++ have “with” keyword like Pascal?

拜拜、爱过 提交于 2019-11-28 10:50:25
with keyword in Pascal can be use to quick access the field of a record. Anybody knows if C++ has anything similar to that? Ex: I have a pointer with many fields and i don't want to type like this: if (pointer->field1) && (pointer->field2) && ... (pointer->fieldn) what I really want is something like this in C++: with (pointer) { if (field1) && (field2) && .......(fieldn) } In C++, you can put code in a method of the class being reference by pointer . There you can directly reference the members without using the pointer. Make it inline and you pretty much get what you want. Probably the

Python multi-line with statement

梦想的初衷 提交于 2019-11-28 07:59:37
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 with Dummy() as a, Dummy() as b, Dummy() as c: pass Unfortunately, that is a SyntaxError . So I tried this: with (Dummy() as a, Dummy() as b, Dummy() as c): pass Also a syntax error. However, this worked: with Dummy() as a, Dummy() as b,\ Dummy() as c: pass But what if I wanted to place a comment? This does not work:

Meaning of “with” statement without “as” keyword

梦想与她 提交于 2019-11-28 06:15:13
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 whatever other finalization routine a class may present. I recently came across a piece of code dealing with OpenGL that presented this: with self.shader: (Many OpenGL commands) Note that absence of any as keyword. Does this indicate that the __enter__ and __exit__ methods of the class are still

Conditional with statement in Python

别来无恙 提交于 2019-11-28 05:12:32
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 encased in the with statement, while another possibility would be the same block, but not encased (i.e., as if it wasn't indented) Initial experiments of course give indentation errors.. If you want to avoid duplicating code and are using a version of Python prior to 3.7 (when contextlib.nullcontext was introduced) or even

Catching exception in context manager __enter__()

六月ゝ 毕业季﹏ 提交于 2019-11-28 04:33:45
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, e_val, trcbak): ... print "This isn't running" ... >>> with TstContx(): ... pass ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in __enter__ Exception: Oops in __enter__ >>> Edit This is as close as I could get... class TstContx(object): def __enter__(self): try: # __enter__ code except Exception as e self.init_exc

Break or exit out of “with” statement?

断了今生、忘了曾经 提交于 2019-11-28 04:22:20
I'd just like to exit out of a with statement under certain conditions: with open(path) as f: print 'before condition' if <condition>: break #syntax error! print 'after condition' Of course, the above doesn't work. Is there a way to do this? (I know that I can invert the condition: if not <condition>: print 'after condition' -- any way that is like above?) The best way would be to encapsulate it in a function and use return : def do_it(): with open(path) as f: print 'before condition' if <condition>: return print 'after condition' with giving you trouble? Throw more with -able objects at the

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

百般思念 提交于 2019-11-28 03:42:49
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 making my scripts into a package. I will update again when I have some conclusions. Update I am trying to pass a lot of input argument variables to f2 and do not want to index every variable inside the function as env$c, env$d, env$calls , that is why I tried to use with in f5 and f6 (a modified f2 ). However, assign does not work with with inside

UDF returns the same value everywhere

杀马特。学长 韩版系。学妹 提交于 2019-11-28 01:34:41
I am trying to code in moving average in vba but the following returns the same value everywhere. Function trial1(a As Integer) As Variant Application.Volatile Dim rng As Range Set rng = Range(Cells(ActiveCell.Row, 2), Cells(ActiveCell.Row - a + 1, 2)) trial1 = (Application.Sum(rng)) * (1 / a) End Function The ActiveCell property does not belong in a UDF because it changes . Sometimes, it is not even on the same worksheet. If you need to refer to the cell in which the custom UDF function resides on the worksheet, use the Application.Caller method. The Range.Parent property can be used to

Object becomes None when using a context manager

◇◆丶佛笑我妖孽 提交于 2019-11-28 00:50:21
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' Change the definition of X to class X(object): var1 = 1 def __enter__(self): return self def __exit__(self, type, value, traceback): pass with assigns the return value of the __enter__() method to the name after as . Your __enter__() returned None , which was assigned to z . I also changed the class to a new-style class (which is not critical to make it work). See the docs

Getting the block of commands that are to be executed in the with statement

为君一笑 提交于 2019-11-27 23:20:35
In reading the specifications for the with statement ( link ), I have some things I'd like to play around with. This isn't for any production code or anything, I'm just exploring, so please don't be too harsh if this is a bad idea. What I'd like to do is grab the piece called "BLOCK" in the linked docs above, and actually tinker around with it inside of the call to __enter__ . (See the linked doc, just after the start of the motivation and summary section.) The idea is to create my own sort of on-the-fly local namespace. Something like this: with MyNameSpace(some_object): print a #Should print