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

    Jul 11, 2016 · Modification of the global variable from inside a function. To point the global variable at a different object, you are required to use the global keyword again: def …

  2. python - How to make a local variable (inside a function) global ...

    That said, "global" is a keyword in python, so you can designate a particular variable as a global, like so: def foo(): global bar bar = 32 I should mention that it is extremely rare for the 'global' …

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

    You can think of Python global variables as "module" variables - and as such they are much more useful than the traditional "global variables" from C. A global variable is actually defined in a …

  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 …

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

    Aug 30, 2015 · Omit the 'global' keyword in the declaration of coins outside the function. This article provides a good survey of globals in Python. For example, this code: def f(): global s …

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

    Dec 30, 2009 · First, the only global variables Python really has are module-scoped variables. You cannot make a variable that is truly global; all you can do is make a variable in a particular …

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

    May 13, 2021 · If you think about it, the way the global declaration works in Python is clearly woven into and consistent with how Python normally works. It's just when you actually want a …

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

    Jul 12, 2017 · 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 …

  9. Defining lists as global variables in Python - Stack Overflow

    Jan 16, 2015 · Instead, you need do declare that you intend to use the global x: x = 0 def f(): global x x = 1 f() print x #=>1 But note that assignment of a variable is very different from …

  10. How do you declare a global constant in Python?

    Jun 10, 2014 · The issue with all of the above solution is that you can by mistake re-set this global variable defined in global.py or module.py in your current file. The better solution would be to …

Refresh