scope

Python3 global scoping in different modules

雨燕双飞 提交于 2021-02-08 06:44:48
问题 I would like to clarify, how globals from different modules are scoped? I failed to find relevant documentation for this matter, thus I am relying on observation, but I would like to have some more insight, if my findings are pure implementation coincidence, or if I can trust them? Testcase: module_1.py: global_var1 = 'm1_v1' global_var2 = 'm1_v2' def f(): print('f: {}'.format(global_var1)) print('f: {}'.format(global_var2)) module_2.py: import module_1 global_var1 = 'm2_v1' def g(): print('g

What does the call stack look like in a system of nested functions, functions passed as arguments, and returned functions?

时间秒杀一切 提交于 2021-02-08 06:33:13
问题 Here is a made up example of nested functions with functions passed as parameters and functions returned: function foo() { let x = 100 function bar(a, b) { function mul(cb) { let m = x * a cb(m) } function add(cb) { let m = x + b cb(m) } function update(m) { x = m } mul(update) add(update) console.log(x) } return bar } let bar = foo() bar(2, 3) // => 203 bar(2, 3) // => 409 bar(2, 3) // => 821 bar(2, 3) // => 1645 Without figuring out potential ways in which these functions could be optimized

How to use mutate_at() with two sets of variables, in R

*爱你&永不变心* 提交于 2021-02-07 20:13:48
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

痞子三分冷 提交于 2021-02-07 20:01:30
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

五迷三道 提交于 2021-02-07 20:00:54
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

How to use mutate_at() with two sets of variables, in R

旧街凉风 提交于 2021-02-07 20:00:51
问题 Using dplyr, I want to divide a column by another one, where the two columns have a similar pattern. I have the following data frame: My_data = data.frame( var_a = 101:110, var_b = 201:210, number_a = 1:10, number_b = 21:30) I would like to create a new variable: var_a_new = var_a/number_a, var_b_new = var_b/number_b and so on if I have c, d etc. My_data %>% mutate_at( .vars = c('var_a', 'var_b'), .funs = list( new = function(x) x/(.[,paste0('number_a', names(x))]) )) I did not get an error,

In Julia, is there a way to pass a variable from a local scope to the enclosing local scope?

我的梦境 提交于 2021-02-07 19:42:24
问题 I am writing some Julia code, wrapped in a function for performance. I need to pass a variable created in a loop to an outer loop but would like to avoid globals for performance reasons: function f() for i=1:1 for j=1:1 a=2 end println(a) end end f() This throws an error since the scope of the i-loop does not know about the variable a . Definining a first within the scope in question works: function f() for i=1:1 a=0 for j=1:1 a=2 end println(a) end end f() I am not super happy with that

c++ : calling constructors via curly braces?

蹲街弑〆低调 提交于 2021-02-07 18:48:19
问题 class A { int value_; public: A(int value):value_(value){} }; A get_a1(int value) { return A(value); } A get_a2(int value) { return {value}; } int main() { A a1 = get_a1(1); A a2 = get_a2(2); } What is the difference between get_a1() and get_a2() , if any ? How is return {value}; called ? (I guess "calling constructors via curly braces" is not the proper way to refers to this) 回答1: In your case, there is simply no difference. But if you modify your code a bit, there will be a visible

c++ : calling constructors via curly braces?

谁都会走 提交于 2021-02-07 18:46:12
问题 class A { int value_; public: A(int value):value_(value){} }; A get_a1(int value) { return A(value); } A get_a2(int value) { return {value}; } int main() { A a1 = get_a1(1); A a2 = get_a2(2); } What is the difference between get_a1() and get_a2() , if any ? How is return {value}; called ? (I guess "calling constructors via curly braces" is not the proper way to refers to this) 回答1: In your case, there is simply no difference. But if you modify your code a bit, there will be a visible

PowerShell - execute script block in specific scope

余生长醉 提交于 2021-02-07 12:12:04
问题 I am trying to implement RSpec/Jasmine like BDD framework in Powershell (or at least research the potential problems with making one). Currently I am having problems with implementing simple before/after functionality. Given $ErrorActionPreference = "Stop" function describe() { $aaaa = 0; before { $aaaa = 2; }; after { $aaaa; } } function before( [scriptblock]$sb ) { & $sb } function after( $sb ) { & $sb } describe the output is 0, but I would like it to be 2. Is there any way to achieve it