scope

Lexical scoping in a for loop enclosing a promise?

南笙酒味 提交于 2019-12-31 05:27:10
问题 I have an ids object, which maps id strings to product objects. for id of ids product = ids[id] console.log product # Prints out something different each loop. :) Product.create(product).then -> console.log product # Only prints out the last id each loop. :( I'm using a library for database interactions, which exposes promises (indicated by the then function above). I'm trying to print out the product variable inside the then function, but I only seem to be getting the last id in ids , so it

Access public static class' state from a separate class file

人走茶凉 提交于 2019-12-31 04:42:09
问题 I have a public static class within another public class as follows: public class Foo<A> { public static class Bar<A>{ A firstBar; Bar(A setBar){ this.firstBar=setBar; } } public final Bar<A> instanceBar; public Foo(A actualValue) { instanceBar = new Bar<A>(actualValue); } public Bar<A> getBar() { return instanceBar; } My objective is to access instanceBar 's state from a separate class file without a get method and without changing the visibility of firstBar . How do I accomplish this? For

Why Cython forces declaration of locals at the beginning of a function

一世执手 提交于 2019-12-31 03:54:08
问题 This was asked as a comment in Cython - copy constructors. The following code doesn't compile in Cython: def bar(int i): if i == 0: return i else: cdef int j j = i+1 return j whereas this one is perfectly correct: def foo(int i): cdef int j if i == 0: return i else: j = i+1 return j The question is: why does Cython forces to declare j at the beginning of the function and not in the else block ? 回答1: The reason is scoping rule in Python vs C/C++. Cython is trying to get the better of both

Nested function definitions and scope (UnboundLocalError)

北慕城南 提交于 2019-12-31 03:35:06
问题 Why is the following code invalid: def foo1(x=5): def bar(): if x == 5: x = 6 print(x) bar() While this code is valid: def foo2(x=5): def bar(): if x == 5: print('ok') print(x) bar() foo2() will do exactly what you expect, but foo1() will give a UnboundLocalError: local variable 'x' referenced before assignment at the line if x == 5: . Why does altering the value of x later on in the code make this conditional invalid? 回答1: Python needs first to detect what variables are local , and which

UnboundLocalError when manipulating variables yields inconsistent behavior

大兔子大兔子 提交于 2019-12-31 03:33:06
问题 In Python, the following code works: a = 1 b = 2 def test(): print a, b test() And the following code works: a = 1 b = 2 def test(): if a == 1: b = 3 print a, b test() But the following does not work: a = 1 b = 2 def test(): if a == 1: a = 3 print a, b test() The result of this last block is an UnboundLocalError message, saying a is referenced before assignment. I understand I can make the last block work if I add global a in the test() definition, so it knows which a I am talking about. Why

Why am I getting “is not a class or namespace” error

与世无争的帅哥 提交于 2019-12-31 03:04:10
问题 I have a file that is generating the error "Block" is not a class or a namespace name on the line typedef Block::point point; However, I'm positive that it's a class that I've both created and #included in the file below 'texture.h'. #pragma once #include "Block.h" #include <iostream> #include <vector> #include <GL/glut.h> #include "lodepng.h" using namespace std; typedef Block::point point; class Texture { public: Texture(int width, int height, string filename);//initialises our texture and

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

让人想犯罪 __ 提交于 2019-12-31 03:02:55
问题 I have defined max() function as below: max <- function(...) max(...,na.rm=T) But it fails to compute max(1:5) with following error: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Watching the result in traceback() identifies the problem: 88: max(..., na.rm = T) at PositionMeth.R#1521 87: max(..., na.rm = T) at PositionMeth.R#1521 86: max(..., na.rm = T) at PositionMeth.R#1521 85: max(..., na.rm = T) at PositionMeth.R#1521 84: max(..., na.rm = T) at

PHP Undefined variable in functions and included scripts

久未见 提交于 2019-12-31 02:49:29
问题 I've read a LOT of things about this problem, however I still cannot fix it. In my functions file I declare a variable with a value like so: $px_host = "localhost"; And I have a database query function like so: function dbQuery($database, $reqquery){ if(!$connect = mysql_connect($px_host, $px_dbuser, $px_dbpass)){ exit("Error - cannot connect to MySQL server - " . mysql_error()); } if(!$database = mysql_select_db($database)){ exit("Error - cannot select database - " . mysql_error()); } if(!

Javascript: every event-handler defined in for-loop is the same, uses last iteration's values

﹥>﹥吖頭↗ 提交于 2019-12-31 02:18:26
问题 I have trouble understanding the scoping rules in Javascript. In the example below, I would assume that scope url variable is private in the for-loop. And that the onload-event function would see this private instance. But things does not seems work like that - the alert will popup with the last url twice. If somebody can clarify what is going on, I'll be grateful. <html> <head> </head> <body> <script type="text/javascript"> var testArray = ["http://g0.gstatic.com/images/icons/onebox/weather

Accessing variable inside object property

那年仲夏 提交于 2019-12-31 01:59:10
问题 So I encountered a problem. I have this object called myTree . And that object has properties. One of the properties contains a method like this: prep: function (variable) { /* some code */ } In that method there is an array myarray and I want to know, whether it is possible to access the content of that array, and if it is, how I would do that. I've made a demo on jsFiddle, and in the end of the JavaScript window you can see that I'm alerting the object prep in which myarray is contained.