scope

Global dictionaries don't need keyword global to modify them? [duplicate]

喜欢而已 提交于 2019-12-17 09:22:10
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: Why is not the keyword global not required in this case? I wonder why I can change global dictionary without global keyword? Why it's mandatory for other types? Is there any logic behind this? E.g. code: #!/usr/bin/env python3 stringvar = "mod" dictvar = {'key1': 1, 'key2': 2} def foo(): dictvar['key1'] += 1 def bar(): stringvar = "bar" print(stringvar) print(dictvar) foo() print(dictvar) print(stringvar) bar(

Why can't we define a variable inside an if statement?

陌路散爱 提交于 2019-12-17 09:06:15
问题 Maybe this question has been answered before, but the word if occurs so often it's hard to find it. The example doesn't make sense (the expression is always true), but it illustrates my question. Why is this code valid: StringBuilder sb; if ((sb = new StringBuilder("test")) != null) { Console.WriteLine(sb); } But this code isn't: if ((StringBuilder sb = new StringBuilder("test")) != null) { Console.WriteLine(sb); } I found a similar question regarding a while statement. The accepted answer

javascript variable scope in the IF statement

☆樱花仙子☆ 提交于 2019-12-17 08:56:08
问题 Are variables declared and assigned in an "if" statement visible only within that "if" block or within the whole function? Am I doing this right in the following code? (seems to work, but declaring "var structure" multiple times seems awkward) any cleaner solutions? function actionPane(state) { if(state === "ed") { var structure = { "element" : "div", "attr" : { "class" : "actionPane" }, "contains" : [{ "element" : "a", "attr" : { "title" : "edit", "href" : "#", "class" : "edit" }, "contains"

Why can't I assign values to global variables outside a function in C?

与世无争的帅哥 提交于 2019-12-17 07:51:31
问题 Suppose I have a global variable, and I want to assign another variable to it. I've found out that you can assign another value to a global variable inside a function: int i = 8; int main(void) { i = 9; /* Modifies i */ return 0; } However, assignment of the global variable outside of a function does not work! int i = 8; i = 9; /* Compiler error */ int main(void) { return 0; } I get the following error message: warning: data definition has no type or storage class warning: type defaults to

JavaScript Variable Scope [duplicate]

五迷三道 提交于 2019-12-17 07:51:23
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . I'm having a problem with some JavaScript code. Script setTimeout(function() { for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 200); } }, 200); Outputs 5, 5, 5, 5, 5 instead of 1, 2, 3, 4, 5 I can kind of understand why this doesn't work, but I was wondering if someone could explain to me what's happening and why it's not working!

JavaScript Variable Scope [duplicate]

一个人想着一个人 提交于 2019-12-17 07:51:11
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 5 years ago . I'm having a problem with some JavaScript code. Script setTimeout(function() { for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, i * 200); } }, 200); Outputs 5, 5, 5, 5, 5 instead of 1, 2, 3, 4, 5 I can kind of understand why this doesn't work, but I was wondering if someone could explain to me what's happening and why it's not working!

Why can't I assign values to global variables outside a function in C?

一笑奈何 提交于 2019-12-17 07:51:03
问题 Suppose I have a global variable, and I want to assign another variable to it. I've found out that you can assign another value to a global variable inside a function: int i = 8; int main(void) { i = 9; /* Modifies i */ return 0; } However, assignment of the global variable outside of a function does not work! int i = 8; i = 9; /* Compiler error */ int main(void) { return 0; } I get the following error message: warning: data definition has no type or storage class warning: type defaults to

Servlet redirect to same page with error message

Deadly 提交于 2019-12-17 07:15:24
问题 I have a question about servlet redirection to the same initial page. The following is the scenario: Suppose a user want to buy an item, so he fills in the amount and submits it. The form is submitted to a servlet and the quantity available is checked against the available in the database. So if the amount of items ordered is more than the available the servlet redirects to the same page but with a message like "item is unavailable". So my question is how to implement this case. How to

Object not found error with ddply inside a function

南笙酒味 提交于 2019-12-17 07:15:12
问题 This has really challenged my ability to debug R code. I want to use ddply() to apply the same functions to different columns that are sequentially named; eg. a, b, c. To do this I intend to repeatedly pass the column name as a string and use the eval(parse(text=ColName)) to allow the function to reference it. I grabbed this technique from another answer. And this works well, until I put ddply() inside another function. Here is the sample code: # Required packages: library(plyr) myFunction <-

Servlet redirect to same page with error message

你离开我真会死。 提交于 2019-12-17 07:15:03
问题 I have a question about servlet redirection to the same initial page. The following is the scenario: Suppose a user want to buy an item, so he fills in the amount and submits it. The form is submitted to a servlet and the quantity available is checked against the available in the database. So if the amount of items ordered is more than the available the servlet redirects to the same page but with a message like "item is unavailable". So my question is how to implement this case. How to