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

    Jul 11, 2016 · 1. assign variable inside functions and use global line. def declare_a_global_variable(): global global_variable_1 global_variable_1 = 1 # Note to use the …

  2. Correct Use Of Global Variables In Python 3 - Stack Overflow

    I'm not downvoting, but I see a problem with this answer: The OP's #2 example is actually incorrect: The global keyword is not needed within a function unless the function is assigning a …

  3. python - Using global variables between files? - Stack Overflow

    If instead of thinking of a "global variable" as being a "global" variable and instead think of it as a "module" variable - then things become clear. You can import things from a module from other …

  4. When do I need to use the global keyword in python

    Aug 8, 2012 · In effect, you have two different variables named bar: the global and the local. Saying global bar tells Python to use the global version of bar rather than creating a new local …

  5. python - Why are global variables evil? - Stack Overflow

    Oct 3, 2013 · This has nothing to do with Python; global variables are bad in any programming language. However, global constants are not conceptually the same as global variables; global …

  6. How can I access global variable inside class in Python

    May 13, 2021 · Q: How can I access a global variable from inside a class in Python? A: By declaring it global inside the function that accesses it. Why?: The global statement is a …

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

    Dec 30, 2009 · If you assign the variable without the global keyword then Python creates a new local var -- the module variable's value will now be hidden inside the function. Use the global …

  8. How do I use global variables in python functions? [duplicate]

    global: 1 f: 1 global: 1 g: 2 global: 1 h: 3 global: 3 Basically you use a global variable when you need every function to access the same variable (object). This isn't always the best way though.

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

    The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local …

  10. Python function global variables? - Stack Overflow

    You can directly access a global variable inside a function. If you want to change the value of that global variable, use "global variable_name". See the following example: var = 1 def …

Refresh