scoping

Julia scoping: why does this function modify a global variable?

独自空忆成欢 提交于 2021-01-28 02:32:20
问题 I'm a relative newcomer to Julia, and so far I am a fan of it. But coming from years of R programming, some scoping rules leave me perplexed. Let's take this function. This behaves exactly as I would expect. function foo1(x) y = x t = 1 while t < 1000 t += 1 y += 1 end return 42 end var = 0; foo1(var) # 42 var # 0 But when doing something similar on an array, it acts as a mutating function (modifies it argument in the global scope!) function foo2(x) y = x t = 1 while t < 1000 t += 1 y[1] += 1

Julia scoping: why does this function modify a global variable?

随声附和 提交于 2021-01-27 22:51:45
问题 I'm a relative newcomer to Julia, and so far I am a fan of it. But coming from years of R programming, some scoping rules leave me perplexed. Let's take this function. This behaves exactly as I would expect. function foo1(x) y = x t = 1 while t < 1000 t += 1 y += 1 end return 42 end var = 0; foo1(var) # 42 var # 0 But when doing something similar on an array, it acts as a mutating function (modifies it argument in the global scope!) function foo2(x) y = x t = 1 while t < 1000 t += 1 y[1] += 1

Pass-by-reference differences in C++ vs Python

南笙酒味 提交于 2021-01-01 09:18:24
问题 I have a little bit of C++ background. I am currently learning Python and am trying to understand how the function parameters are passed. I know this question is asked numerous times up here, but since I am not a Computer Science major, a lot of discussions threads are rather esoteric to me, plus, I've not seen a lot of comparisons on this topic between different languages, so I thought I'd give a new post a try. The situation is this: My understanding is that Python only pass by reference (i

Can't use locals() in list comprehension in Python 3?

有些话、适合烂在心里 提交于 2020-03-03 17:58:54
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

Can't use locals() in list comprehension in Python 3?

眉间皱痕 提交于 2020-03-03 17:58:20
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

Can't use locals() in list comprehension in Python 3?

本小妞迷上赌 提交于 2020-03-03 17:58:10
问题 The below works in Python 2 but not 3. Is there a way to access local variables in Python 3? Or an alternative solution to these cases? [('{name_var}_{i:02d}of{maxpg:02d}.{date_var}').format(i, **locals()) for i in range(start, end)] Error in Python 3: KeyError: 'local_var' Below is a simpler toy example of above (works in Python 2 but not 3) local_var = 'hello' ['{local_var}'.format(**locals()) for i in range(1)] Error in Python 3: KeyError: 'local_var' 回答1: As explained by @user2357112 in a

Lambda variable scope

孤者浪人 提交于 2020-01-24 03:18:40
问题 Example: myObject.Stub(s => s.MyMethod(null)).IgnoreArguments().Return("bleh"); var s = "s"; A variable "s" is defined in a lambda and another variable "s" as a local variable within the same method. Visual Studio tells me "A conflicting variable is defined below" when I hover over the first "s". Why are these conflicting; the "s" in the lambda is not available outside of its enclosing brackets surely? 回答1: They are conflicting because a rule of C# is that any two uses of the same simple name

Accessing variable in another function, returns undefined - JavaScript

 ̄綄美尐妖づ 提交于 2020-01-24 01:02:48
问题 I am trying to access a variable which exists in another function, but I am not able to, it gives me undefined for the function through which (getMess() as below) I am doing that. As per the code below, I want the "value1" accessed through myfunction1, as shown below. Code: var namespace ={ myfunction1: function(){ namespace.myfunction2.getMess(); // I need to access value1 here in this function }, myfunction2: function(message1,message2){ var value1 = message1; var value2 = message2; return{

R: Source personal scripts keeping some functions hidden

流过昼夜 提交于 2020-01-17 05:06:44
问题 Follow up to this I want to source scripts inside a given environment, like in sys.source , but "exporting" only some functions and keeping the others private . I created this function: source2=function(script){ ps=paste0(script, "_") assign(ps, new.env(parent=baseenv())) assign(script, new.env(parent=get(ps))) private=function(f){ fn=deparse(substitute(f)) assign(fn, f, parent.env(parent.frame())) rm(list=fn, envir=parent.frame()) } assign("private", private, get(script)) sys.source(paste0

xtext importURI external file

旧时模样 提交于 2020-01-17 03:59:17
问题 i am very lost with how importing global scope is working. I am writing a parser with xtext for a well structure language and I cannot depend on workspace or project concepts. In my language I have include statements which can have relative or absoloute paths of another file. I need to be able to reference objects defined in this other file. I tried using importURI but it doesn't work. All I tried was to add this rule: Include: 'INCLUDE' '=' importURI=STRING ';' ; and changed the MWE2 file in