with-statement

with statement - backport for Python 2.5

时光怂恿深爱的人放手 提交于 2019-12-23 15:25:06
问题 I'd like to use with statement in Python 2.5 in some production code. It was backported, should I expect any problems (e.g. with availability/compatibility on other machines/etc)? Is this code from __future__ import with_statement compatible with Python 2.6? 回答1: with_statement wasn't back ported but implemented in Python 2.5. Adding new keywords or syntax can break existing applications. With Python the way they decided to handle this is allow people to opt-in to those features early so you

How come eval doesn't have access to the scoped variables under a with statement?

大兔子大兔子 提交于 2019-12-23 08:26:59
问题 Why can't you access scoped variables using eval under a with statement? For example: (function (obj) { with (obj) { console.log(a); // prints out obj.a eval("console.log(a)"); // ReferenceError: a is not defined } })({ a: "hello" }) EDIT : As the knowledgeable CMS pointed out, this appears to be a browser bug (browsers that use the WebKit console). If anyone was wondering what abomination I was trying to come up with that would require both the "evil" eval and with -- I was trying to see if

Why with() in R is doing vector operation in one case and not in the other? [duplicate]

扶醉桌前 提交于 2019-12-23 04:24:59
问题 This question already has answers here : Boolean operators && and || (3 answers) Closed 6 years ago . I have the following table : > head(datalist[[5]]) X5CO X5CS X5CD X5CSD 1 24.87769 24.31233 26.84647 34.3316 2 24.74026 24.31233 26.84647 34.3316 3 24.45217 24.31233 26.84647 34.3316 10 24.87769 24.31233 26.15139 34.3316 11 24.74026 24.31233 26.15139 34.3316 12 24.45217 24.31233 26.15139 34.3316 I need to apply the following expression using every row as variable values. So I'm using with()

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

妖精的绣舞 提交于 2019-12-21 13:21:35
问题 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 回答1: Just use try-finally. Really, this may be nice as a mental exercise, but if you actually do it in code you care about you will end up with ugly, hard to maintain code. 回答2: You could (ab)use decorators to do this, I think. The following works, eg: def

python's `with` statement target is unexpectedly None

被刻印的时光 ゝ 提交于 2019-12-18 19:05:47
问题 seems like I do not understand something with---the python with statement. Consider this class: class test(object): def __enter__(self): pass def __exit__(self, *ignored): pass now, when using it with with , like in with test() as michael: print repr(michael) I would expect some output like <test instance at memore blah> . But I get None . Something wrong here? Any suggestions would help. (I am using Python 2.6.6.) EDIT: Thanks to ephement for pointing me to the documentation. The __enter__

What does “with” do in JavaScript?

巧了我就是萌 提交于 2019-12-18 11:29:43
问题 I saw JavaScript code which begins with with . That's a bit confusing. What does it do and how can it be used correctly? with (sObj) return options[selectedIndex].value; 回答1: It adds to the scope of the statements contained in the block: return sObj.options[selectedIndex].value; can become: with (sObj) return options[selectedIndex].value; In your case, it doens't do a whole lot...but consider the following: var a, x, y; var r = 10; a = Math.PI * r * r; x = r * Math.cos(PI); y = r * Math.sin

Is there a way to debug VB.NET With clause in Visual Studio?

限于喜欢 提交于 2019-12-18 08:29:58
问题 Sometimes you don't really care about the name of a variable, because it does not go outside of your sub's scope. In fact, specifying the name would add an extra line of code. Also now you have this name to deal with, which may add to potential refactoring effort (if you decide to rename it later). Have a look at the code below: Dim fileInfo As New FileInfo("filename.txt") With New FileSystemWatcher .Path = fileInfo.DirectoryName .Filter = fileInfo.Name .EnableRaisingEvents = True AddHandler

Scope of variable within “with” statement?

核能气质少年 提交于 2019-12-18 04:42:10
问题 I am reading only firstline from python using : with open(file_path, 'r') as f: my_count = f.readline() print(my_count) I am bit confused over scope of variable my_count. Although prints work fine, would it be better to do something like my_count = 0 outside with statement first (for eg in C in used to do int my_count = 0 ) 回答1: A with statement does not create a scope (like if , for and while do not create a scope either). As a result, Python will analyze the code and see that you made an

File open and close in python

放肆的年华 提交于 2019-12-18 04:08:15
问题 I have read that when file is opened using the below format with open(filename) as f: #My Code f.close() explicit closing of file is not required . Can someone explain why is it so ? Also if someone does explicitly close the file, will it have any undesirable effect ? 回答1: The mile-high overview is this: When you leave the nested block, Python automatically calls f.close() for you. It doesn't matter whether you leave by just falling off the bottom, or calling break / continue / return to jump

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

元气小坏坏 提交于 2019-12-17 19:32:53
问题 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) } 回答1: In C++, you can put code in a method of the class being reference by pointer . There you can directly reference the