-2

I'm trying to create a global variable, but from what I know you need to type "global" in every function.

This example is in Python 2.7:

a = 10

def change():
    global a
    a = 2
    print a

def reverse():
    global a
    a = 10
    print a

// Output:
// 2
// 4

I'm trying to re-create the code above without the use of global for every variable in every function I am going to create. Is there a better way to do this?

To better explain this situation, I have a dictionary and I intend to use many different functions on it. The problem is, I don't really want to be using function(dict) or do_this(dict) each time, I prefer function() or do_this(). My thinking is that if dict was global, I could solve this problem.

P.S Thank you for all the answers! Not exactly what I was looking for, but I appreciate the answers.

gyre
  • 16,369
  • 3
  • 37
  • 47
Yuval Meshorer
  • 156
  • 2
  • 8
  • 1
    Possible duplicate of [Using global variables in a function other than the one that created them](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – gyre Apr 24 '17 at 19:33
  • 4
    The language designers aren't particularly interested in making global variables convenient. – user2357112 Apr 24 '17 at 19:34
  • 2
    In general, avoid global variables (especially if you have to use `global`, even just once). Use classes. – Alex Hall Apr 24 '17 at 19:34
  • Maybe use a dictionary to store values at the module level. – wwii Apr 24 '17 at 19:36
  • Don't use `global`, basically ever. It's bad – Markus Meskanen Apr 24 '17 at 19:36

2 Answers2

1
class Globo(object):
    pass


def change():
    Globo.a = 2
    print Globo.a
Serge
  • 3,387
  • 3
  • 16
  • 34
  • Your indentation is a bit off ... try this: https://gist.github.com/benhoyt/8d69954e619a5961a37369275d6239c0 :-) – Ben Hoyt Apr 24 '17 at 19:47
  • Thought about same joke, but yours has one argument more. If knows how to use function parameters, what is for the globals? – Serge Apr 24 '17 at 19:50
1

If you just want to read the variable (not modify it), you don't need the global line. See the offical FAQ. However, if you really want to circumvent this rule, you can make a global dictionary (or list or other mutable data structure) and put your global variables in there. Then you can manipulate them through the global dictionary:

a = 4
globs = dict(g1=3)


def print_a():
    print(a)  # not modifying, so you don't need `global`
    globs['g1'] += 1  # technically not modifying globs either.
    print(globs)

print_a() # 4 \n {'g1', 4}
print(globs) # {'g1', 4}

Why does this work? (I'm going to explain the best I know here, but please feel free to edit if you know better). Replace "modify" with "reassign" in your head. When you change the value of a (a += 1 for example), you're actually reassigning a so Python won't let you. When you're changing a value in globs, you're not actually reassigning anything to the name globs, so it lets you. A link that may help you:

The thing that you can't do in a function without declaring global is to change the arrow from the global namespace to point to something else.

Ben
  • 5,952
  • 4
  • 33
  • 44