so i faced strange behaviour with this code below. I am getting error that local variable flag is referenced before assignment, but its assigned at the top as global variable. Can someone tell me whats going here and why flag is not incrementing as expected?  import concurrent.futures   flag = 0 def make_some():     try:         flag += 1     except Exception as e:         print(e)  with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:     tasks = {         executor.submit(             make_some         ): task_id         for task_id in [1,2,3]     }     for future in concurrent.futures.as_completed(tasks):         pass
1022
The following code works as expected in both Python 2.5 and 3.0:  a, b, c = (1, 2, 3)  print(a, b, c)  def test():     print(a)     print(b)     print(c)    # (A)     #c+=1       # (B) test()   However, when I uncomment line (B), I get an UnboundLocalError: 'c' not assigned at line (A). The values of a and b are printed correctly. This has me completely baffled for two reasons:   Why is there a runtime error thrown at line (A) because of a later statement on line (B)? Why are variables a and b printed as expected, while c raises an error?   The only explanation I can come up with is that a local variable c is created by the assignment c+=1, which takes precedent over the "global" variable c even before the local variable is created. Of course, it doesn't make sense for a variable to "steal" scope before it exists.  Could someone please explain this behavior?
58090
In  it came to the question about the difference between local and global in a TeX meaning.  In the liked question the babel shorthand ; should be deactivated inside a {tikzpicture}, which can be done with  \begin{tikzpicture}    \shorthandoff{;}    ... \end{tikzpicture}
48999
What is the way to refer index variable inside looping ?  The code below results in error:     Failed to save undefined: expecting a positive integer, found 'indx'   at column 10 of expression: v.labels[indx]: Source       &lt;aura:iteration var="item" items="{!v.items}" indexVar="indx"&gt;         &lt;li&gt;             &lt;label class="card-detail-label"&gt;{!v.labels[indx]}&lt;/label&gt;             &lt;span class="card-detail-value"&gt;{!item}&lt;/span&gt;           &lt;/li&gt;     &lt;/aura:iteration&gt;
1371
I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference?  A (quite pointless) example in Java:  a) declaration before loop:  double intermediateResult; for(int i=0; i &lt; 1000; i++){     intermediateResult = i;     System.out.println(intermediateResult); }   b) declaration (repeatedly) inside loop:  for(int i=0; i &lt; 1000; i++){     double intermediateResult = i;     System.out.println(intermediateResult); }   Which one is better, a or b?   I suspect that repeated variable declaration (example b) creates more overhead in theory, but that compilers are smart enough so that it doesn't matter. Example b has the advantage of being more compact and limiting the scope of the variable to where it is used. Still, I tend to code according example a.  Edit: I am especially interested in the Java case.
12929
Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway.  I just want a very simple prime generator (nothing fancy).   import math  def main():     count = 3     one = 1     while one == 1:         for x in range(2, int(math.sqrt(count) + 1)):             if count % x == 0:                  continue             if count % x != 0:                 print count          count += 1
27582
I have a set of global counter variables in Javascript:  var counter_0 = 0; var counter_1 = 0; var counter_2 = 0;   etc  I then have a Javascript function that accepts an 'index' number that maps to those global counters. Inside this function, I need to read and write to those global counters using the 'index' value passed to the function.  Example of how I'd like it to work, but of course doesn't work at all:  function process(index) {     // do some processing      // if 'index' == 0, then this would be incrementing the counter_0 global variable     ++counter_+index;       if (counter_+index == 13)     {         // do other stuff     } }   I hope what I'm trying to accomplish is clear. If not I'll try to clarify. Thanks.  EDIT Clarification:  I'm not trying to increment the name of the counter, but rather the value the counter contains.
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]
86426
The title says it all (I'm referring to the case, when writing for example an article to be published or something similar - something where the writing itself should also be of quality).  Example: "Let $A_1,A_2,\ldots,A_n$ be subsets of a set $L$ and $f:L\rightarrow L$. Then define $B=\cup_{i=0}^n A_i$ and $C=\cup_{i=1}^{\infty} f^i (A_2)$, where $f^i$ is $f$ iterated with itself $i$ times."  So (in this example), would it be a problem to use $i$ twice as an index variable the ranges over different sets ? Or is it ok to think of $i$ as being define like a (lets do a computer-science analogy) "local variable", that is only used to create the object "in" which it is defined ($B$ for example) and after the object is created, the variable is "freed" again (so it can be used again to define $C$) ?  If it is NOT okay to do that, when is it allowed to use the same symbol again, after its first use ? After half a page ? After two pages ? In index-heavy proofs one would very soon have finished all letters commonly used as indices, of one would always have to use a different letter. And indexing sets with letters like $x$ or $f$ seems even worse style to me (ok, maybe there are areas in math, in which this would be common practice, but I don't know any).
123679
Two function definitions, the only difference is that the first combines the local storage keyword with the assignment, while the second separates them:  function foo {     local fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  function bar {     local fn     fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  foo bar   This echoes "0" then "1". I expect it to echo "1" then "1". It seems like the value of $? is the result of the assignment to local, rather than the result of the command substitution.  Why does bash 4.2.46(1)-release behave this way?
7750
Can anybody give a clear explanation of how variable assignment really works in Makefiles.   What is the difference between :   VARIABLE = value  VARIABLE ?= value  VARIABLE := value  VARIABLE += value   I have read the  in GNU Make's manual, but it still doesn't make sense to me.
7441
Was there any reason why the designers of Java felt that local variables should not be given a default value? Seriously, if instance variables can be given a default value, then why can't we do the same for local variables?  And it also leads to problems as explained in :     Well this rule is most frustrating when trying to close a resource in a finally block. If I instantiate the resource inside a try, but try to close it within the finally, I get this error. If I move the instantiation outside the try, I get another error stating that a it must be within a try.      Very frustrating.
9770
I'm sure you all know the behaviour I mean - code such as:  Thread thread = new Thread(); int activeCount = thread.activeCount();   provokes a compiler warning.  Why isn't it an error?  EDIT:  To be clear: question has nothing to do with Threads.  I realise Thread examples are often given when discussing this because of the potential to really mess things up with them.  But really the problem is that such usage is always nonsense and you can't (competently) write such a call and mean it.  Any example of this type of method call would be barmy.  Here's another:  String hello = "hello"; String number123AsString = hello.valueOf(123);   Which makes it look as if each String instance comes with a "String valueOf(int i)" method.
122254
I need to update a variable defined in a pgfmathsetmacro command inside a foreach loop. Apparently, the var is only updated when it is used after the update command. Here is a MWE:  \begin{tikzpicture} \draw [help lines] (0,0) grid (3,3); \pgfmathsetmacro{\a}{1} \foreach \b in {2,3} {     \node [blue] at (\a,0) {\a};     \pgfmathsetmacro{\a}{\b}     \node [red] at (0,\a) {\a}; } \end{tikzpicture}   The red nodes are printed at updated positions and the value printed is also updated; the blue nodes are not: they keep their initial value (1) and are located at the initial position (0,1).  Why is this? Thanks José
67728
Is there any way of doing parallel assignment in C++? Currently, the below compiles (with warnings)  #include &lt;iostream&gt;   int main() {    int a = 4;   int b = 5;   a, b = b, a;   std::cout &lt;&lt; "a: " &lt;&lt; a &lt;&lt; endl             &lt;&lt; "b: " &lt;&lt; b &lt;&lt; endl;    return 0; }   and prints:  a: 4 b: 5   What I'd like it to print ... if it weren't obvious, is:  a: 5 b: 4   As in, say, ruby, or python.
51358
How do you decide between passing arguments to a method versus simply declaring them as object instance variables that are visible to all of the object's methods? I prefer keeping instance variables in a list at the end of the Class, but this list gets longer as my program grows. I figure if a variable is passed often enough it should just be visible to all methods that need it, but then I wonder, &quot;if everything is public there will be no need for passing anything at all!&quot;
118792
The following code  using System.Threading;  class Test {     volatile int counter = 0;     public void Increment()     {         Interlocked.Increment(ref counter);     } }   Raises the following compiler warning:  "A reference to a volatile field will not be treated as volatile"   Am I doing something wrong here to raise this warning? Why does the compiler me warn about this?
49826
I've been trying to wrap my head around how threads work in Python, and it's hard to find good information on how they operate. I may just be missing a link or something, but it seems like the official documentation isn't very thorough on the subject, and I haven't been able to find a good write-up.  From what I can tell, only one thread can be running at once, and the active thread switches every 10 instructions or so?  Where is there a good explanation, or can you provide one? It would also be very nice to be aware of common problems that you run into while using threads with Python.