global

Python: Why is global needed only on assignment and not on reads?

南楼画角 提交于 2019-12-03 19:46:10
问题 If a function needs to modify a variable declared in global scope, it need to use the global declaration. However, if the function just needs to read a global variable it can do so without using a global declaration: X = 10 def foo(): global X X = 20 # Needs global declaration def bar(): print( X ) # Does not need global My question is about the design of Python: why is Python designed to allow the read of global variables without using the global declaration? That is, why only force

How do I query DynamoDB2 table by global secondary index only using boto 2.25.0?

半城伤御伤魂 提交于 2019-12-03 19:07:31
This is a continuation** of my quest to switch from regular DynamoDB tables to DynamoDB2 ones with Global Secondary Indices. So I created my table as shown here and then added the following two elements: table.put_item(data={'firstKey': 'key01', 'message': '{"firstKey":"key01", "comments": "mess 1 w/o secondKey"}'}) table.put_item(data={'firstKey': 'key02', 'secondKey':'skey01', 'message': '{"firstKey":"key02", "parentId":"skey01", "comments": "mess 2 w/ secondKey"}'}) What I want to do now is retrieve items by either their (i) unique firstKey values or (ii) unique secondKey values. The 1st

How can I avoid global state?

十年热恋 提交于 2019-12-03 17:31:36
问题 So, I was reading the Google testing blog, and it says that global state is bad and makes it hard to write tests. I believe it--my code is difficult to test right now. So how do I avoid global state? The biggest things I use global state (as I understand it) for is managing key pieces of information between our development, acceptance, and production environments. For example, I have a static class named "Globals" with a static member called "DBConnectionString." When the application loads,

PHP avoid static classes to avoid dependencies, but I need to use global everywhere

三世轮回 提交于 2019-12-03 17:29:28
Many times I heard to avoid static classes because they will insert dependencies that will render your code unusable in other projects, and will not allow to unit test it . Let's say we have a typical class DB to access the Data Base, if such class is static we could call it wherever in our code: DB::execQuery(...); but this creates dependencies, so let's make the DB class NOT static, in such case we would have somewhere in our code: $db = new DB(); and then we could call in our code $db->execQuery(...); But now when using the $db inside a function we need each time to first declare it like

global constants without using #define

帅比萌擦擦* 提交于 2019-12-03 16:29:46
问题 Ok, I'm looking to define a set of memory addresses as constants in a .h file that's used by a bunch of .c files (we're in C, not C++). I want to be able to see the name of the variable instead of just seeing the hex address in the debugger... so I want to convert the #defines I currently have into constants that are global in scope. The problem is, if I define them like this: const short int SOME_ADDRESS = 0x0010 then I get the dreaded "multiple declarations" error since I have multiple .c

ActiveRecord Global Callbacks for all Models

天涯浪子 提交于 2019-12-03 16:14:23
I have around 40 models in my RoR application. I want to setup a after_save callback for all models. One way is to add it to all models. Since this callback has the same code to run, is there a way to define it globally once so that it gets invoked for all models. I tried this with no luck: class ActiveRecord::Base after_save :do_something def do_something # .... end end Same code works if I do it in individual models. Thanks, Imran You should use observers for this: class AuditObserver < ActiveRecord::Observer observe ActiveRecord::Base.send(:subclasses) def after_save(record) AuditTrail.new

How do I create a Solution Wide Connection String

拜拜、爱过 提交于 2019-12-03 16:12:24
Does anyone know if it is possible to create a single connection string that will be accessible to all the projects in a solution (we have about 6). I can create a text file with this information, but we need design time support as well, and it is not practical to have a connection string in every App.Config and Web.config file in the solution. We basically want a Single connection string that is easy to change should the location of the db change, that will also be used by the IDE for design time support mafu Not sure, can't you just put a new config file to the solution items and include and

global variable in octave

ε祈祈猫儿з 提交于 2019-12-03 14:24:35
global m = 1; function p = h() m end h() I'm trying to run this script, but I get this error: 'm' undefined near line 4 column 3 Say me please, how I can use the variable from functions? You have to declare the var also global inside the function as described here: https://www.gnu.org/software/octave/doc/interpreter/Global-Variables.html global m = 1; function p = h() global m; m endfunction h() 来源: https://stackoverflow.com/questions/27409364/global-variable-in-octave

jQuery/Javascript: Defining a global variable within a function?

五迷三道 提交于 2019-12-03 13:48:39
I have this code: var one; $("#ma1").click(function() { var one = 1; }) $("body").click(function() { $('#status').html("This is 'one': "+one); }) and when I click the body, it says: This is 'one': undefined. How can I define a global variable to be used in another function? Remove the var from inside the function. $("#ma1").click(function() { one = 1; }) If you want to make a global variable bind it to window object window.one = 1; var one;//define outside closure $("#ma1").click(function() { one = 1; //removed var }) $("body").click(function(e) { $('#status').html("This is 'one': "+one); })

Store (almost) all objects in workspace in a list

扶醉桌前 提交于 2019-12-03 13:03:48
问题 Let's say that I have many objects in my workspace (global environment) and I want to store most of those in a list. Here's a simplified example: # Put some objects in the workspace A <- 1 B <- 2 C <- 3 I would like to store objects A and C in a list. Of course, I can do that explicitly: mylist <- list(A,C) However, when the number of objects in the workspace is very large, this would become rather cumbersome. Hence, I would like to do this differently and attempted the following: mylist <-