scope

Python functions can be given new attributes from outside the scope?

↘锁芯ラ 提交于 2020-01-14 02:23:56
问题 I didn't know you could do this: def tom(): print "tom's locals: ", locals() def dick(z): print "z.__name__ = ", z.__name__ z.guest = "Harry" print "z.guest = ", z.guest print "dick's locals: ", locals() tom() #>>> tom's locals: {} #print tom.guest #AttributeError: 'function' object has no attribute 'guest' print "tom's dir:", dir(tom) # no 'guest' entry dick( tom) #>>> z.__name__ = tom #>>> z.guest = Harry #>>> dick's locals: {'z': <function tom at 0x02819F30>} tom() #>>> tom's locals: {}

The `arguments` object changes if parameters change

二次信任 提交于 2020-01-14 01:38:55
问题 I just discovered that the arguments object actually changes if one of the parameters change. For example: function some(a, b, c ){ console.log(arguments); args = [ a, b, c ]; a = new Date(); console.log(arguments); console.log(args); } some(1,2,3 ); You will see that while args stays the same (expected behaviour), arguments actually change. Questions: Is this something that is well documented? If so, where? Is there anything else I need to be careful about the arguments object? 回答1: This is

Android Static Variable Scope and Lifetime

让人想犯罪 __ 提交于 2020-01-13 12:07:32
问题 I have an application that has a Service that uses an ArrayList<Double> to store numbers in the background for a very long time; the variable is initialized when the service started. The service is in the background, and there will be frequent access to the variable (that's why I don't want to use file management or settings -- it will be very expensive for a file I/O for the sake of battery life). The variable will likely be ~1MB->2MB over its lifetime. Is it safe to say that the variable

JavaScript Modules, Closures and Scope

大兔子大兔子 提交于 2020-01-13 11:31:34
问题 I am using the following closure pattern to modularise my code: (function(root) { // MODULE CODE HERE if (typeof module !== 'undefined' && module.exports) { // CommonJS /* var dependencies = require(...) */ module.exports = myModule; } else if (typeof define !== 'undefined' && define.amd) { // AMD /* var dependencies...; */ define([/* dependencies */], function(/* dependencies */) { /* Assign closure level vars to respective arguments */ return myModule; }); } else { // Dependencies?? root

Where is nonlocals()?

一曲冷凌霜 提交于 2020-01-13 11:05:54
问题 How do I obtain the non-local variables for the current scope? The functions vars , locals , and globals exist, but is there a function to get the nonlocals ? Why aren't the nonlocals listed when calling vars ? Update My issue is that there's no way to enumerate the variables available in the current scope, as neither vars or globals includes the non-locals AFAICT. I frequently use vars in code such as the following: '{meh[0]}/{meh[3]} {a}{b}{c}'.format(**vars()) Which fails if any of these

Where is nonlocals()?

こ雲淡風輕ζ 提交于 2020-01-13 11:05:14
问题 How do I obtain the non-local variables for the current scope? The functions vars , locals , and globals exist, but is there a function to get the nonlocals ? Why aren't the nonlocals listed when calling vars ? Update My issue is that there's no way to enumerate the variables available in the current scope, as neither vars or globals includes the non-locals AFAICT. I frequently use vars in code such as the following: '{meh[0]}/{meh[3]} {a}{b}{c}'.format(**vars()) Which fails if any of these

Does a function expression have its own scope/lexical environment

痴心易碎 提交于 2020-01-13 10:25:09
问题 I'm reading the Execution Context / Lexical Environment section of the ECMA 262 5 specification. It states the following: (emphasis added) A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with

What is the JavaScript variable scope in a switch / case statement?

我怕爱的太早我们不能终老 提交于 2020-01-13 08:23:11
问题 While creating JavaScript with ASP.NET MVC I noticed several scope warnings and realized that I am missing something with understanding the variable scope inside the switch / case statement. Warning: 'i' is already defined referring to case b and case c My code looks similar to this: switch(element) { case 'a': for(var i=0; i < count; i++){ do something } break; case 'b': for(var i=0; i < count; i++){ do something } break; case 'c': for(var i=0; i < count; i++){ do something } break; } I

CDI - ApplicationScoped but configured

空扰寡人 提交于 2020-01-13 07:20:33
问题 Problem Using CDI I want to produce @ApplicationScoped beans. Additionally I want to provide a configuration annotation to the injection points, e.g.: @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface Configuration { String value(); } I do not want to write a separate producer for each different possibility of value . Approach The usual way would be to make a producer and handle the injection point annotations:

CDI - ApplicationScoped but configured

安稳与你 提交于 2020-01-13 07:20:13
问题 Problem Using CDI I want to produce @ApplicationScoped beans. Additionally I want to provide a configuration annotation to the injection points, e.g.: @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface Configuration { String value(); } I do not want to write a separate producer for each different possibility of value . Approach The usual way would be to make a producer and handle the injection point annotations: