14

I'm relatively new to thinking of python in terms of object-oriented programming, and it's coming relatively slowly to me.

Is it possible to pass global variables between classes with multiple functions in them? I've read extensively about them here and in other sources, but I'm still a bit confused.

My ultimate goal is to define a variable globally, assign it a value in a function of one class, and then USE it in another class's function. Is that possible? I'm building a pythonaddin for ArcMap, which requires a class for each button. I want to have a button to assign a value to a variable, and then use that variable in another class in the script.

(Before I get the flak, I know it's relatively bad form to use global variables in the first place, I'm trying to learn)

For instance, as a highly simplified example:

x = []

class A():

    def func_1(self): 
        #populate the x variable
        global x 
        x = 1


class B():

    def func_2(self):
        global x
        #do stuff with x

Is this acceptable (even if not pythonic)?

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
user22861
  • 141
  • 1
  • 1
  • 5

3 Answers3

9

Yes, it can work. Here is the modified code:

x = []

class A:
    def func_1(self):
        #populate the x variable
        global x 
        x.append(1)
class B:
    def func_2(self):
        global x
        print x

a = A()
a.func_1()
b = B()
b.func_2()

It can print the list x correctly. When it's possible, you should try to avoid global variables. You can use many other ways to pass variables between classes more safely and efficiently.

PS: Notice the parameters in the functions.

Old Panda
  • 1,466
  • 2
  • 15
  • 30
  • Alright, well I guess my issue is that I was getting confused calling the variable from one class into another. How would that work? Pardon for the pretty simple questions../. – user22861 Nov 24 '13 at 22:44
  • Oh wait, I just figured it out. It's just a matter of saving a variable as self.x and then calling the B.x later. Thanks for the guidance! – user22861 Nov 24 '13 at 23:14
  • I think you can initialize another class in one class. Then use the variables in another class using its functions. Just like `class A: def __init__(self): self.another = B()` – Old Panda Nov 24 '13 at 23:43
2

A more Pythonic solution would be to have the button objects (in their method that handles the "press" event) set an attribute or call a method of whatever object needs the information.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • That's what I tried to do, but I got a little bogged down when calling a variable from a class in one module into another. What form would that take? – user22861 Nov 24 '13 at 22:39
  • @user22861 You would have to create an instance of the target class, e.g. named `spam` (instance, not class) in its module e.g. `eggs`. In the module that uses the buttons you would then do `from eggs import spam`. – Roland Smith Nov 25 '13 at 00:31
0

Yes, it is possible. Your code is almost right, just missing a couple of parenthesis in the function definitions. Other than that, it works.

Now, is that acceptable? In 99% of cases, no, it's not. There're many ways of passing variables and sharing data between classes, and using global variables is one of the worse ones, if not the worst.

José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
  • 4
    Just wondered... Why 'globals' are bad solution? For example - i'm now workin with code, whcih will have really lot of variables (about 20), which must be used in lot of functions && classes && methods. So - why it's bad - just set them as globals? And - what solution then: use classes iheritanace (some parent `Main` class with `__init__`, which will set default var's for all 'child' classes && methods? Is it solution? P.S. In my case - all var's will be `const`, I mean - they wil lsetup when script started, and not changed till finish. – setevoy Feb 22 '15 at 09:27
  • 6
    this answer needs to be elaborated. – bonobo Mar 04 '20 at 19:53