scope

Can we access inner function outside its scope of outer function in python using outer function?

耗尽温柔 提交于 2019-12-10 13:53:46
问题 Just for the sake of curiosity I wanna know this.. I know scope of inner function is limited to outer function body only, but still is there any way so that we can access the inner function variable outside its scope or call the inner function outside its scope ? In [7]: def main(): ...: def sub(): ...: a=5 ...: print a ...: In [8]: main() In [9]: main.sub() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home

Why Javascript doesn't let a function redefine itself from within itself?

独自空忆成欢 提交于 2019-12-10 13:50:20
问题 Consider the code: window.a = function(x){ var r = x*2; window.a =alert; // redefines itself after first call return r; }; a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4" This doesn't work either: window.a = function(x){ alert(x); window.a = function(x){ // redefines itself after first call var r = x*2; return r; } }; a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4" As neither does this: window.a = function(x){ alert(x); window.c = window.a; window.a

PHP 5 - Variable scope across include files with no classes or functions

假如想象 提交于 2019-12-10 13:46:37
问题 I've read through many, many threads on this and still can't wrap my head around it. Here's my basic issue: header.php includes a file called navigation.php . Within navigation.php , $previous and $next are defined. Using echo statements I have verified they have values. Next, header.php includes a file called backnext.php . I need backnext.php to know the values of $previous and $next . If I declare them as global at the top of backnext.php , I don't get errors, but echo statements show they

Function declaration or function expression

假如想象 提交于 2019-12-10 13:39:54
问题 I just ran into a problem when defining a function in a block scope. Consider the following program: try { greet(); function greet() { alert("Merry Christmas!"); } } catch (error) { alert(error); } I expected this program to alert Merry Christmas! . However in Firefox is gives me the following ReferenceError : ReferenceError: greet is not defined On Opera and Chrome it alerts the greeting as I expected it to. Evidently Firefox treats the function inside the block scope as a FunctionExpression

foreach, performance-wise. Should we declare variable once before loop or inside it?

情到浓时终转凉″ 提交于 2019-12-10 13:28:34
问题 What is better for performance wise declaring the variable outside the foreach statment and the each time reassign it in side it (foreach) or create an new variable inside foreach for example private List<ListItem> GetItems() { var items = new List<ListItem>(); var collection = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ListItem item; foreach (var i in collection) { item = new ListItem { Text = i.ToString() }; items.Add(item); } return items; } or this one? private List<ListItem>

Compiler Error variable “Not captured” inside lambda function for threadpool

我的梦境 提交于 2019-12-10 13:24:48
问题 I'm learning about multithreading in c++ and am trying to set up a thread pool, but am getting a complier error saying "error: ‘mapperNodes’ is not captured" and "error: ‘command’ is not captured". I've read a bit about using "this" to capture the variables in the lambda, but so far nothing has been working. How can I use the command and mapperNoders variables in the thread pool lambda function in the code below? void MapReduceServer::spawnMappers() throw() { vector<string> mapperNodes(nodes)

PHP global variable scope inside a class

流过昼夜 提交于 2019-12-10 12:46:57
问题 I have the following script myclass.php <?php $myarray = array('firstval','secondval'); class littleclass { private $myvalue; public function __construct() { $myvalue = "INIT!"; } public function setvalue() { $myvalue = $myarray[0]; //ERROR: $myarray does not exist inside the class } } ?> Is there a way to make $myarray available inside the littleclass, through simple declaration? I don't want to pass it as a parameter to the constructor if that was possible. Additionally, I hope that you

How to pass object context to an anonymous function?

荒凉一梦 提交于 2019-12-10 12:44:07
问题 Is there a way of passing object context to an anonymous function without passing $this as an argument? class Foo { function bar() { $this->baz = 2; # Fatal error: Using $this when not in object context $echo_baz = function() { echo $this->baz; }; $echo_baz(); } } $f = new Foo(); $f->bar(); 回答1: You can assign $this to some variable and then use use keyword to pass this variable to function, when defining function, though I'm not sure if it is easier to use. Anyway, here's an example: class

make variable available to all classes, methods, functions and includes, just like $_POST

穿精又带淫゛_ 提交于 2019-12-10 12:42:21
问题 This question seems simple enough, but I can't find an answer anywhere... At the beginning of my php script/file I want to create a variable. $variable = 'this is my variable'; I want this variable to be available within the entire script, so that all classes, methods, functions, included scripts, etc. can simple call this variable as $variable. Class MyClass { public function showvariable() { echo $variable; } } The $_SESSION, $_POST, $_GET variables all behave like that. I can just write a

How to create a scope to order cities name?

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:49:50
问题 I have two models/tables: Services and City Service model: belongs_to :origin_city, class_name: 'City' belongs_to :destiny_city, class_name: 'City' How do I create a scope to order cities name? I'm trying something like that: scope :by_city_name, -> { joins(:city).order("cities.name asc") } But I just want to order the service origin city in one scope. And service destiny city in another scope. 回答1: So you should join only origin_city : scope :by_origin_city_name, -> { joins(:origin_city)