About 311,000 results
Open links in new tab
  1. How can I use a global variable in a function? - Stack Overflow

    Jul 11, 2016 · When you assign 42 to the name _my_global, therefore, Python creates a local variable that shadows the global variable of the same name. That local goes out of scope and is garbage …

  2. Why does assigning to my global variables not work in Python?

    If you don't assign to a in the function (as with printA ()) then Python uses the global variable A. To mark a variable as global you need to use the global keyword in Python, in the scope that you want to use …

  3. Python function global variables? - Stack Overflow

    Within a Python scope, any assignment to a variable not already declared within that scope creates a new local variable unless that variable is declared earlier in the function as referring to a globally …

  4. How to declare a global variable in Python correctly

    Jan 28, 2023 · If a function assigns to a variable name (which your greet() function does for the player variable), then that variable name is treated as local everywhere in the function, even if there is a …

  5. Python - Easiest way to define multiple global variables

    Dec 6, 2016 · The reason for this is that python treats classes as modules, so this works out well for setting up a list of global variables- such as when trying to make an interface with multiple options …

  6. How to set a global variable in Python - Stack Overflow

    Ok, so I've been looking around for around 40 minutes for how to set a global variable on Python, and all the results I got were complicated and advanced questions and more so answers. I'm trying t...

  7. Why can't I set a global variable in Python? - Stack Overflow

    Aug 15, 2009 · If you actually meant to refer to a global variable, as in your question, you need the global keyword to tell Python that's what you meant. If you don't assign to the variable anywhere in …

  8. How to create module-wide variables in Python? [duplicate]

    Dec 30, 2009 · The global keyword is not necessary if you only access but do not assign the variable in the function. If you assign the variable without the global keyword then Python creates a new local …

  9. Insert variable into global namespace from within a function?

    28 But be aware that assigning function variables declared global only injects into the module namespace. You cannot use these variables globally after an import:

  10. python - Dynamic global variables assignment - Stack Overflow

    python dynamic global-variables variable-assignment edited Oct 6, 2017 at 17:30 martineau 124k 29 181 319