Questions tagged [local-variables]

Local variables have a limited scope, generally one function or one functional block.

Local variables have a limited scope, generally one function or one functional block.

Compare with global variables, which are accessible from any part of a program.

1302 questions
6
votes
3 answers

NameError: undefined - have parsing rules for local variables changed in Ruby 2.1.2?

I am getting NameError: undefined local variable or method with ruby 2.1.2 As observed in this question, expressions like: bar if bar = true raises an undefined local variable error (provided that bar is not defined prior) because bar is read by…
sawa
  • 165,429
  • 45
  • 277
  • 381
6
votes
1 answer

Speed of global and local variables in Python

I guess everyone knows that: Python accesses local variables much more efficiently than global variables Ok, it's true: oldlist = [str(i) for i in range(500)] oldlist1 = [i for i in range(500)] %%timeit newlist = [] for word in oldlist: …
6
votes
2 answers

Can another thread see partially created collection when using collection initializer?

Imagine this C# code in some method: SomeClass.SomeGlobalStaticDictionary = new Dictionary() { {0, "value"}, }; Let's say no one is using any explicit memory barriers or locking to access the dictionary. If no optimization takes…
Palo
  • 1,051
  • 8
  • 12
6
votes
3 answers

(python) Should my variable be local or global? (best practice)

When declaring a constant that is only used one function, should that variable be declared locally since it is only used by that function, or globally since it is never going to change? IE which is better: CONSTANT = (1, 3, 5, 8) ##SOME OTHER CODE…
6
votes
2 answers

Output buffering vs. storing content into variable in PHP

I don't know exactly how output buffering works but as far as know it stores content into some internal variable. Regarding this, what is the difference of not using output buffering and storing content in my own local variable instead and than…
Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91
6
votes
2 answers

Scope of final local variable in java

Method-Local inner class cannot access local variables because the instance of the method-local inner class may still alive after the method is over. But local variables will vanish once the local method is over. I learned that method-local inner…
ZZZ
  • 3,574
  • 10
  • 34
  • 37
6
votes
4 answers

Make A javascript variable only avaliable to functions on the same .js file

I somewhat new to Javascript and I'm stuck on this one item. Can someone please show me how to make a javascript variable only usable on the .js file that it is on. EXAMPLE: Say I have two .js files, PG1 & PG2. PG1 contains var channel =…
6
votes
2 answers

How to add event handler to local variable in VB.NET

I have a form in VB.NET that is used as a dialog in a mainform. Its instances are always locally defined, there's no field for it. When the user clicks the OK button in the dialog, it will fire an event with exactly one argument, an instance of one…
MarioDS
  • 12,895
  • 15
  • 65
  • 121
5
votes
2 answers

Can someone explain these few lines of MSIL?

Can someone explain these few lines of MSIL? Why does it move a value off the evaluation stack to a local variable, only to move it back immediately and return it? The following MSIL code loads a single argument (a string), calls a method, which…
Triynko
  • 18,766
  • 21
  • 107
  • 173
5
votes
2 answers

Passing local variable with name of a global variable isn't possible in JS?

foo = "foobar"; var bar = function(){ var foo = foo || ""; return foo; } bar();` This code gives a result empty string. Why cannot JS reassign a local variable with same name as a global variable? In other programming languages the expected…
GirginSoft
  • 375
  • 3
  • 16
5
votes
1 answer

How returning a string_view of a local literal works

Consider this snippet: #include #include #include using namespace std::literals; class A { public: std::string to_string() const noexcept { return "hey"; // "hey"s } std::string_view…
MatG
  • 574
  • 2
  • 7
  • 19
5
votes
3 answers

Perl map block local variable usage

This code compiles a set by way of hash keys of the unique basename stubs in a set of paths. %stubs = map { $f=basename $_; $f =~ /^([A-Za-z]+[0-9]+)\./ ; $1=>() } @pathlist; Why do I need the $f references here? I thought I'd be ok with: %stubs =…
Phil H
  • 19,928
  • 7
  • 68
  • 105
5
votes
4 answers

Does an int in Objective-C have a default value of 1?

I have this simple line of code: int x; x automatically has the value of 1. I don't set it to anything but when I debug, it shows that x is 1. Does an int have a default value of 1?!
aryaxt
  • 76,198
  • 92
  • 293
  • 442
5
votes
3 answers

C# reusable function to dump current value of local variables

I would like to write a reusable function I can call within any method to log a snapshot of all the local variables. For example: void somemethod() { int a = 1; string s = "something"; dumpLocalVariables("step 1",…
hsmiths
  • 1,257
  • 13
  • 17
5
votes
1 answer

Unbound local variable problem in Python

I've got a following code snippet: def isolation_level(level): def decorator(fn): def recur(level, *args, **kwargs): if connection.inside_block: if connection.isolation_level < level: raise…
julx
  • 8,694
  • 6
  • 47
  • 86