0

I have this very nasty python script which is, to say the least not very well managed and I'd like to improve it. The way I thought about doing that is breaking up the code into what they do and then importing them into the main function that runs everything. But some of my defs have a global (like the key word based global) in them, how do I scope those out in separate files exactly?

for example main,py wi have:

import function

message = {}

while (true):
    function.function(message)
    print(message)

and function,py has :

def function(some variables) :

    global message

    if (somevariable = something):
        message = "xyz"

message in the second file is giving me an error

EDIT: So I see that I need to give a bit more context, there are infact 3 processes (functions) that are running and the global message is what I'm using to pass information between all these threads. So Ideally Id like to separate all the processes as different files and then keep adding them as a new thread. How do I go about this?

Indrajeet Haldar
  • 163
  • 1
  • 13
  • 2
    Declare `message` in `function.py` instead. Then, in `main.py`, refer to it as `function.message`. – Green Cloak Guy Jul 15 '21 at 15:15
  • To access any name imported, you use `modulename.name`. So you call `function.function()` and `function.message` – Barmar Jul 15 '21 at 15:15
  • 1
    This is going to be very difficult at scale if you don't have tests to check your work. If you don't, my first step would be writing some – Ben Jul 15 '21 at 15:16
  • Would you guys suggest just keeping this as in a single file? @GreenCloakGuy the issue is that I'm using message in multiple modules (which have different responsibilities, so there will be some 5 message = function1.message , function2.message etc – Indrajeet Haldar Jul 15 '21 at 15:21
  • `each module has its own "global" namespace` https://stackoverflow.com/questions/15890014/namespaces-with-module-imports – Epsi95 Jul 15 '21 at 15:22
  • You should refactor the function and classes in other modules and call from the main module, so ideally instead of declaring message global you are supposed to pass message to `def function(somevariables, message)` then use the variable inside the function. – Epsi95 Jul 15 '21 at 15:24
  • okay@Epsi95 but that doesn't solve the issue of passing in the module's globals in the main function for me to mess around with – Indrajeet Haldar Jul 15 '21 at 15:24
  • ah yeah thats not exactly possible I think ? I guess I need to go back to the drawing board and plan the code out – Indrajeet Haldar Jul 15 '21 at 15:25
  • So Im a bit lost - In reality I have multiple treads which are running and the message variable is basically being used to pass information between them since its defined in the global scope. what is the alternative if I want to write each one of the processes as an individual file and run the function but still pass information between each one of the threads? – Indrajeet Haldar Jul 15 '21 at 17:01

0 Answers0