11When I run one function, at the end, the variable 'hangarDone' is changed to 'True' (its is false at the start) but when I run the morrigan function it prints as false.  def morrigan():     print ('Hang2',hangarDone)     hangar()  def hangar():     [There is content here but nothing happens with hangarDone]     hangarDone = 'True'     save()     print ('Hang1',hangarDone)     morrigan()   Output:  Hang2 False Hang1 True Hang2 False
2152
What exactly are the Python scoping rules?  If I have some code:  code1 class Foo:    code2    def spam.....       code3       for code4..:        code5        x()   Where is x found?  Some possible choices include the list below:   In the enclosing source file In the class namespace In the function definition In the for loop index variable Inside the for loop   Also there is the context during execution, when the function spam is passed somewhere else. And maybe  pass a bit differently?  There must be a simple reference or algorithm somewhere.  It's a confusing world for intermediate Python programmers.
36147
The following method does not work because the inner block declares a variable of the same name as one in the outer block. Apparently variables belong to the method or class in which they are declared, not to the block in which they are declared, so I therefore can't write a short little temporary block for debugging that happens to push a variable in the outer scope off into shadow just for a moment:  void methodName() {   int i = 7;   for (int j = 0; j &lt; 10; j++) {     int i = j * 2;   } }   Almost every block-scoped language I've ever used supported this, including trivial little languages that I wrote interpreters and compilers for in school.  Perl can do this, as can Scheme, and even C.  Even PL/SQL supports this!  What's the rationale for this design decision for Java?  Edit: as somebody pointed out, Java does have block-scoping.  What's the name for the concept I'm asking about?  I wish I could remember more from those language-design classes. :)
4059
The situation is somewhat like-  var someVar = some_other_function(); someObj.addEventListener("click", function(){     some_function(someVar); }, false);   The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.
30825
I'm writing an operator for which it is crucial that every time it gets called all the properties are set to their default values.   The ops.object.delete has 1 param. use_global. It is always false, no matter what the user put in last time it got called. I would like to replicate exactly that.   (For example: I want to modify delta scale. The default val for my_factor is 1, so nothing happens until the user alters that number. If the user calls the operator again, it will have stored whatever number the user has used last, however I want it to always start with 1.)
37233
Let me use the following example to explain my question: public string ExampleFunction(string Variable) {     return something; }  string WhatIsMyName = &quot;Hello World&quot;; string Hello = ExampleFunction(WhatIsMyName);  When I pass the variable WhatIsMyName to the ExampleFunction, I want to be able to get a string of the original variable's name. Perhaps something like: Variable.OriginalName.ToString()  Is there any way to do this?
73786
I'd like to update the label of an operator when it's called. But this :  def execute(self,context):     self.bl_label = "good night"   ... apparently doesn't work if the bl_label property has already been defined. Am I wrong ?
14937
Sometimes it seems natural to have a default parameter which is an empty list. Yet . If for example, I have a function: def my_func(working_list=[]):     working_list.append(&quot;a&quot;)     print(working_list)  The first time it is called the default will work, but calls after that will update the existing list (with one &quot;a&quot; each call) and print the updated version. So, what is the pythonic way to get the behavior I desire (a fresh list on each call)?
119239
I have a variable, x, and I want to know whether it is pointing to a function or not.  I had hoped I could do something like:  &gt;&gt;&gt; isinstance(x, function)   But that gives me:  Traceback (most recent call last):   File "&lt;stdin&gt;", line 1, in ? NameError: name 'function' is not defined   The reason I picked that is because  &gt;&gt;&gt; type(x) &lt;type 'function'&gt;
73636
I want to make a toggle button for my custom operator. It works similar to checkbox: once the button is clicked, the operator keeps running until the button gets another click, so will the button itself greyed out. I tried to achieve the grey-out effect by implementing poll() function like this:  @classmethod def poll(cls, context):           return context.area.type == 'VIEW_3D' and context.region.type == 'WINDOW' and plane_toggle_flag   Every time the button is clicked I want the toggle state to change, so in the operator's init(), I write:  def __init__(self):        plane_toggle_flag=not plane_toggle_flag   However, it seems as long as poll() returns false, the init() function never get the chance to be called. Any idea how to solve this? Thank you in advance.
25759
I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g.  if [ "x$A" = "xtrue" ]; then   in order to check whether $A is "true".  I assume this is done to achieve shell compatibility or to work around a longterm bug, an unintuitive behavior, etc. Nothing obvious comes to mind.  Today I figured I want to know the reason, but my research didn't turn up anything. Or maybe it's just me making something out of a rather frequent exposure to rare occurances.  Is this practice still useful, maybe even best?
15817
I have enum and use the variables like myEnum.SomeNameA, myEnum.SomeNameB, etc.  When I return one of these variables from a function, can I print their names (such as myEnum.SomeNameA) instead of the value they returned?
46377
While dabbling in Clojure I've written a very basic program to echo whatever the user types into it. However, it doesn't run in a way that I'm perceiving to be natural. Here's the code:  (defn goo []   (print "echo&gt; ")   (def resp (read-line))   (print resp) )   I would expect the code to run like this (for me typing in foo as the input to read-line):  user=&gt; (goo) echo&gt; foo foonil   But instead, the echo and read-line is switched:  user=&gt; (goo) foo echo&gt; foonil   Why does this happen? Is there a subtlety I'm missing?  EDIT: From Joe's answer, the updated correct solution is:  (defn goo []   (print "echo&gt; ")   (flush)   (def resp (read-line))   (print resp)   (flush) )   Also, the flushes aren't necessary if you use println instead of print.
16116
When using IF statements in Python, you have to do the following to make the "cascade" work correctly.  if job == "mechanic" or job == "tech":         print "awesome" elif job == "tool" or job == "rock":         print "dolt"   Is there a way to make Python accept multiple values when checking for "equals to"? For example,  if job == "mechanic" or "tech":     print "awesome" elif job == "tool" or "rock":     print "dolt"
110291
Example: I am looking to see if "arg" is a special keyword in Lua. I go to the Lua reference manual at . I search for "arg". I find hundreds of occurrences of the word "argument(s)" or "vararg".  Any ideas? I know Firefox won't implement whole words as a core feature (something about cluttering up the ui... argh...), and I couldn't find a good addon that implemented this well.
30016
Say I have a variable named choice it is equal to 2. How would I access the name of the variable? Something equivalent to  In [53]: namestr(choice) Out[53]: 'choice'   for use in making a dictionary. There's a good way to do this and I'm just missing it.  EDIT:  The reason to do this is thus. I am running some data analysis stuff where I call the program with multiple parameters that I would like to tweak, or not tweak, at runtime. I read in the parameters I used in the last run from a .config file formated as  filename no_sig_resonance.dat  mass_peak 700  choice 1,2,3   When prompted for values, the previously used is displayed and an empty string input will use the previously used value.  My question comes about because when it comes to writing the dictionary that these values have been scanned into. If a parameter is needed I run get_param which accesses the file and finds the parameter.   I think I will avoid the problem all together by reading the .config file once and producing a dictionary from that. I avoided that originally for... reasons I no longer remember. Perfect situation to update my code!
60984
If you want to use global functions and variable dynamically you can use:  window[functionName](window[varName]);   Is it possible to do the same thing for variables in the local scope?  This code works correctly but currently uses eval and I'm trying to think of how else to do it.  var test = function(){     //this = window     var a, b, c; //private variables      var prop = function(name, def){         //this = window         eval(name+ ' = ' + (def.toSource() || undefined) + ';');              return function(value){             //this = test object             if ( !value) {                 return eval('(' + name + ')');             }             eval(name + ' = value;')             return this;         };      };      return {         a:prop('a', 1),         b:prop('b', 2),         c:prop('c', 3),         d:function(){             //to show that they are accessible via to methods             return [a,b,c];         }     }; }();  &gt;&gt;&gt;test Object &gt;&gt;&gt;test.prop undefined &gt;&gt;&gt;test.a function() &gt;&gt;&gt;test.a() 1 //returns the default &gt;&gt;&gt;test.a(123) Object //returns the object &gt;&gt;&gt;test.a() 123 //returns the changed private variable &gt;&gt;&gt;test.d() [123,2,3]
4021
What is the best way to go about calling a function given a string with the function's name in a Python program.  For example, let's say that I have a module foo, and I have a string whose content is "bar". What is the best way to call foo.bar()?  I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.