scope

R scope: force variable substitution in function without local environment

最后都变了- 提交于 2019-12-18 17:05:26
问题 I'm defining functions in a loop and trying to force evaluation of a loop variable without having to carry around a private environment. Example: a set of functions handlers$h1 , handlers$h2 , ..., handlers$h6 that just pass through the 1, 2, ..., 6 to another function like so: handlers <- list() for (i in 1:6) { handlers[[paste0('h', i)]] <- function () { message(i) # <-- example } } So handlers$h1() should message 1, handlers$h2() should message 2, ... Instead, all of the functions return 6

PHP switch statement variable scope

那年仲夏 提交于 2019-12-18 16:58:34
问题 In PHP, how is variable scope handled in switch statements? For instance, take this hypothetical example: $someVariable = 0; switch($something) { case 1: $someVariable = 1; break; case 2: $someVariable = 2; break; } echo $someVariable; Would this print 0 or 1/2? 回答1: The variable will be the same in your whole portion of code : there is not variable scope "per block" in PHP. So, if $something is 1 or 2 , so you enter in one of the case of the switch , your code would output 1 or 2. On the

Accessing variables with the same name at different scopes [duplicate]

泄露秘密 提交于 2019-12-18 16:52:51
问题 This question already has answers here : Is there any way to access a local variable in outer scope in C++? (4 answers) Closed 4 years ago . With #include <iostream> using namespace std; int a = 1; int main() { int a = 2; if(true) { int a = 3; cout << a << " " << ::a // Can I access a = 2 here? << " " << ::a << endl; } cout << a << " " << ::a << endl; } having the output 3 1 1 2 1 Is there a way to access the 'a' equal to 2 inside the if statement where there is the 'a' equal to 3, with the

Namespacing and classes

我的梦境 提交于 2019-12-18 16:31:24
问题 I'm trying to write some (in my opinion) readable code in Python. I need a module that will contain a number of classes. Theoretically I know everything that is needed to accomplish this: I can simply put class definitions in a single module file. For readability purposes I want to put every class definition into separate file (they are starting to be quite lengthy!), and all these classes into one directory. Whenever I create new file although it's contents are visible where I need them,

Having 2 variables with the same name in a class that extends another class in Java

无人久伴 提交于 2019-12-18 14:54:27
问题 Following is a part of my code for a project: public class Body extends Point{ public double x, y, mass; public Body() { x = y = mass = 0; } public Body(double x, double y, double mass) { this.mass = mass; this.x = x; this.y = y; } } public class Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } } I quickly realized that doing this will create two variables inside the Body class called x and two other variables in Body called y. How is this

Explicit Local Scopes - Any True Benefit?

亡梦爱人 提交于 2019-12-18 14:14:31
问题 I was cleaning up some code and removed an if statement that was no longer necessary. However, I realized I forgot to remove the brackets. This of course is valid and just created a new local scope. Now this got me thinking. In all my years of C# development, I have never come across a reason to use them. In fact, I kind of forgot I could do it. Is there any actual benefit to defining a local scope? I understand I can define variables in one scope and then define the same ones again in an

Explicit Local Scopes - Any True Benefit?

∥☆過路亽.° 提交于 2019-12-18 14:14:15
问题 I was cleaning up some code and removed an if statement that was no longer necessary. However, I realized I forgot to remove the brackets. This of course is valid and just created a new local scope. Now this got me thinking. In all my years of C# development, I have never come across a reason to use them. In fact, I kind of forgot I could do it. Is there any actual benefit to defining a local scope? I understand I can define variables in one scope and then define the same ones again in an

Just when I think I finally understand Javascript scope

不想你离开。 提交于 2019-12-18 13:30:39
问题 I run in to something that illustrates how I clearly don't get it yet. Can anyone please explain why the value of "this" changes in the following? var MyFunc = function(){ alert(this); var innerFunc = function(){ alert(this); } innerFunc(); }; new MyFunc(); 回答1: In JavaScript, this represents the context object on which the function was called , not the scope in which it was defined (or the scope in which it was called). For MyFunc , this references the new object being created; but for

Bash, weird variable scope when populating array with results

本秂侑毒 提交于 2019-12-18 12:53:12
问题 I am parsing command output and putting results into array. Works fine until exiting the inner loop - output array is empty. declare -a KEYS #-----------------------------------------------------------------------------# get_keys() { # this extracts key NAMES from log in format "timestamp keycode" $glue_dir/get_keys $ip | while read line; do echo line: $line set -- $line # $1 timestamp $2 keycode echo 1: $1 2: $2 key=(`egrep "\\s$2$" "$glue_dir/keycodes"`) # tested for matching '40' against

Local variable referenced before assignment in Python

落爺英雄遲暮 提交于 2019-12-18 12:14:22
问题 Truel="" count = 0 finle_touch=False #true after it find the first 3 upperletter # check if there is 1 lower letter after three upper letter def one_lower(i): count=0 if i == i.lower: finle_touch=True Truel=i # check for 3 upper letter def three_upper(s): for i in s: if count == 3: if finle_touch==True: break else: one_lower(i) elif i == i.upper: count +=1 print(count) #for debug else: count ==0 finle_touch=False stuff="dsfsfFSfsssfSFSFFSsfssSSsSSSS......." three_upper(stuff) print(Truel) so