scoping

Strictly speaking does the scoping assignment <<- assign to the parent environment or global environment?

人盡茶涼 提交于 2020-01-03 18:41:08
问题 Often the parent environment is the global environment. But occasionally it isn't. For example in functions within functions, or in an error function in tryCatch() . Strictly speaking, does <<- assign to the global environment, or simply to the parent environment? 回答1: Try it out: env = new.env() env2 = new.env(parent = env) local(x <<- 42, env2) ls(env) # character(0) ls() # [1] "env" "env2" "x" But: env$x = 1 local(x <<- 2, env2) env$x # [1] 2 … so <<- does walk up the entire chain of

Xtext cross referencing and scoping

╄→尐↘猪︶ㄣ 提交于 2020-01-02 19:13:10
问题 I have some problems with xtext cross referencing Here is a very simple grammer: grammar org.xtext.example.mydsl1.Test with org.eclipse.xtext.common.Terminals generate test "http://www.xtext.org/example/mydsl1/Test" Model: block=Block? cs+=Company* ; Block: '{' g=[Employee] '}'; Company: 'Company' name=ID '{' es+= Employee* '}'; Employee: 'Employee' name=ID ';' ; and it is my dsl : { Pooyan } Company Sony{ Employee Pooyan; Employee John; } It always shown that "Couldn't resolve reference to

Xtext cross referencing and scoping

廉价感情. 提交于 2020-01-02 19:13:06
问题 I have some problems with xtext cross referencing Here is a very simple grammer: grammar org.xtext.example.mydsl1.Test with org.eclipse.xtext.common.Terminals generate test "http://www.xtext.org/example/mydsl1/Test" Model: block=Block? cs+=Company* ; Block: '{' g=[Employee] '}'; Company: 'Company' name=ID '{' es+= Employee* '}'; Employee: 'Employee' name=ID ';' ; and it is my dsl : { Pooyan } Company Sony{ Employee Pooyan; Employee John; } It always shown that "Couldn't resolve reference to

Common Lisp scoping (dynamic vs lexical)

折月煮酒 提交于 2019-12-31 10:01:49
问题 EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below: Example 1: (defmethod fun1 (x) (print x) (fun2)) (defmethod fun2 () (print x)) (fun1 5) Output: 5 *** -

Common Lisp scoping (dynamic vs lexical)

折月煮酒 提交于 2019-12-31 10:01:48
问题 EDIT: I changed the example code after the first answer because I came up with a simple version that begs the same questions. I am currently learning Common Lisp's scoping properties. After I thought I had a solid understanding I decided to code up some examples that I could predict the outcome of, but apparently I was wrong. I have three question, each one relating to an example below: Example 1: (defmethod fun1 (x) (print x) (fun2)) (defmethod fun2 () (print x)) (fun1 5) Output: 5 *** -

Why a variable can be accessible outside the loop in Python? [duplicate]

荒凉一梦 提交于 2019-12-31 01:05:08
问题 This question already has answers here : Scoping in Python 'for' loops (6 answers) Closed 4 years ago . Consider this example: for iter in xrange(10): myvar = iter print myvar # 9 Here myvar is clearly outside the loop? But it is still accessible. If this is Perl, it will throw an error. What's the reason behind such feature in Python? Is it harmful? What's the best practice then, to declare a variable before looping? 回答1: There is no new scope created by the for loop (Ruby also behaves the

Python scoping in dict comprehension

人盡茶涼 提交于 2019-12-30 08:18:34
问题 >>> x = 'foo' >>> {0: locals().get('x')} {0: 'foo'} >>> {0: locals().get('x' + spam) for spam in ['']} {0: None} What is the reason for this discrepancy in behaviour? 回答1: Dict comprehensions and generator comprehensions create their own local scope. List comprehensions do not in Python 2.x, but do in Python 3. (Note that your first example is not a dict comprehension. It's just a literal dict that happens to have an expression as the value for the key 0.) 来源: https://stackoverflow.com

Dynamic Scoping - Deep Binding vs Shallow Binding

孤街浪徒 提交于 2019-12-29 04:29:09
问题 I've been trying to get my head around shallow binding and deep binding, wikipedia doesn't do a good job of explaining it properly. Say I have the following code, what would the output be if the language uses dynamic scoping with a) deep binding b) shallow binding? x: integer := 1 y: integer := 2 procedure add x := x + y procedure second(P:procedure) x:integer := 2 P() procedure first y:integer := 3 second(add) ----main starts here--- first() write_integer(x) 回答1: Deep binding binds the

NameError says variable is not defined, but only in some places

时间秒杀一切 提交于 2019-12-25 06:57:57
问题 I am trying to implement a keep-alive that sends some data every 30 seconds to keep a telnet connection open. My code calls reinitScore every second. This function will sometimes call calculateWinner , which sends the data through telnet via stelnet.send(data) . The problem is, when I call stelnet.send(data) inside any function, it raises a NameError: global name 'stelnet' is not defined . My questions is: why would stelnet.send(data) work in one place, and not another? Here is the part of my

Swift scoping issue

怎甘沉沦 提交于 2019-12-25 03:08:15
问题 I have an app that is pulling info from a parse.com database and passing it into an array. When I println() this array from inside the while loop, it prints fine. When I try to print it outside the while loop, it returns empty. Here is my code: var players = [String]() var total = [String]() var addTotal:AnyObject! var addTotalFinal:Int! var addPlayers:AnyObject! var addPlayersFinal:Array<Int>! @IBOutlet weak var tableView: UITableView! var test: AnyObject! override func viewDidLoad() { super