About 300,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 …

  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 …

  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 …

  4. How to declare a global variable in Python correctly

    Jan 28, 2023 · What I'm doing wrong? I expected the code of tic-tac-toe working correctly but it didn't. To resolve the issue I tried to change "player" scope and moved an assignment inside …

  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 …

  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 …

  7. 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 …

  8. 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 …

  9. 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

  10. python - Modifying global variable with same name as local …

    Suppose I have a global variable a. And within a function definition, we also have a local variable named a. Is there any way to assign the value of the global variable to that of the local variabl...