Is it possible to define global variables in a function in Python

70,548

Solution 1

Yes, but why?

def a():
    globals()['something'] = 'bob'

Solution 2

def function(arguments):
    global var_name
    var_name = value #must declare global prior to assigning value

This will work in any function, regardless of it is in the same program or not.

Here's another way to use it:

def function():
    num = #code assigning some value to num
    return num

NOTE: Using the return built-in will automatically stop the program (or the function), regardless of whether it is finished or not.

You can use this in a function like this:

if function()==5 #if num==5:
    #other code

This would allow you to use the variable outside of the function. Doesn't necessarily have to be declared global.

In addition, to use a variable from one function to another, you can do something like this:

import primes as p #my own example of a module I made
p.prevPrimes(10) #generates primes up to n
for i in p.primes_dict:
    if p.primes_dict[i]: #dictionary contains only boolean values
        print p.primes_dict[i]

This will allow you to use the variable in another function or program without having use a global variable or the return built-in.

Share:
70,548
WoooHaaaa
Author by

WoooHaaaa

Updated on July 09, 2022

Comments

  • WoooHaaaa
    WoooHaaaa almost 2 years

    How do I declare a global variable in a function in Python?

    That is, so that it doesn't have to be declared before but can be used outside of the function.

  • Ashwini Chaudhary
    Ashwini Chaudhary over 11 years
    just do, global something, it will create a new global variable if it doesn't exist.
  • Jon Clements
    Jon Clements over 11 years
    explicit rather than implicit... - this makes it more "obvious" as to it's a bad idea...
  • Terrence Brannon
    Terrence Brannon over 11 years
    it's not global is it? I think it's module-wide.
  • Rushy Panchal
    Rushy Panchal over 11 years
    Ah yes, it is only module-wide. However, using the return function should fix that.
  • WoooHaaaa
    WoooHaaaa over 11 years
    @F3AR3DLEGEND globals my_var does usable outside the function, i've tried that.
  • ideasman42
    ideasman42 almost 10 years
    While correct, this isn't standard practice. in most cases its best to use global keyword.
  • TheTechRobo Stands for Ukraine
    TheTechRobo Stands for Ukraine over 3 years
    it may not be standard practice but it saved me a codefactor headache
  • Azmisov
    Azmisov over 3 years
    I believe this is the only way to do it if "something" variable name is programmatically generated
  • TheTechRobo Stands for Ukraine
    TheTechRobo Stands for Ukraine about 3 years
    @Azmisov either that or exec
  • Admin
    Admin over 2 years
    I can think of at least one use-case: I want to mass-produce a bunch of variables in a module (variables whose name would be determined by a set of arguments) and I want to be able to import these variables whenever the module is imported. Is there a cleaner way to do such a thing?