1

I would like to pass around a global variable in python. I know this is not an advisable method, but this belongs to a bigger application where I need to do this.

this is my main.py

from allfiles.subfiles.allclasses import Machine
global a

def func():
   a=5
   print("Inside function:", a)

func()

print("Outside function:", a)

machine = Machine(a)

and this is my allclasses.py

import main

    class Machine():
        def __init__(self, name):
            print("created machine: ", main.a)

see attached picture of the structure...

somehow the import main in the allclasses is a problm. i tried from main import * same problem I have an import error: Cannot import name 'Machine' from 'allfiles.subfiles.allclasses

structure

I just need to pass around this global variable from to allclasses (unidirectional so there will be no cyclic problems) the value is set once (from func) and won t be changed anymore Am I missing something basic here?

EricD1990
  • 33
  • 1
  • 5
  • If you passed `a` in `machine = Machine(a)`, and `Machine.__init__(self, name)` receives it as `name`, why don't you just do `print("created machine: ", name)`? – Thierry Lathuille Mar 30 '21 at 10:26
  • that was just an example. the real thing is more complex. i am more interested into passing the variable of course... – EricD1990 Mar 30 '21 at 10:28
  • It would be good if you could provide an example that more accurately represents your real situation. – Thierry Lathuille Mar 30 '21 at 10:29
  • i simplified it to the maximum to expose my problem. i need to send my variable to allclaases so that it could be used there – EricD1990 Mar 30 '21 at 10:31
  • The actual problem here is the circular import that requires Machine to be defined, which in turn requires `main.a` to already exist, before you create it. – Thierry Lathuille Mar 30 '21 at 10:33
  • ok I see the problem. I tried to put global a on top of the import. so the variable would exist. it still doesnt work. I thought when i run the program from main the starting point would be main – EricD1990 Mar 30 '21 at 10:40
  • 1
    `global` is only meaningful inside a function, if you need to assign to the global variable inside the function (otherwise, a local variable with the same name would be created). Used in global scope, it just does nothing. You might find useful information about circular imports at https://stackoverflow.com/questions/22187279/python-circular-importing. – Thierry Lathuille Mar 30 '21 at 10:45

2 Answers2

0

This still does not solve my problem. the import is done after the definition of the variable. it follows the direction perfectly

   def func():
       global a
       a=5
       print("Inside function:", a)
    
    from allfiles.subfiles.allclasses import Machine
    
    func()
    
    print("Outside function:", a)
    
    machine = Machine()
EricD1990
  • 33
  • 1
  • 5
-1

You can't import two files to each other, I think you should use a database.

  • i read that accessing a global variable can be done like a property that is why i imported main.... – EricD1990 Mar 30 '21 at 10:26
  • Yes, you can have circular imports, see for example https://stackoverflow.com/questions/22187279/python-circular-importing. And I fail to see how using a database would be a reasonable solution to a scope problem... – Thierry Lathuille Mar 30 '21 at 10:31