0

first.py:

from third import *
from second import *

while running:
    off()

second.py:

from third import *

def off():
    running = False

third.py:

running = True

The program still running and running variable is not accessed.

I want to calling a function which is in another file, the function change the boolean which is in another file. I know I can just type everything to the first file but I want separate files for functions and variables.

I expect to close the program after running.

I tried using global variables. I read all similar questions.

1
  • Please don't show images of code, post the code itself. Commented Nov 13, 2022 at 13:00

2 Answers 2

1

Your line: running = False doesn't do what you want it to do.

The key to remember with python is that imports create new variables in the module that does the import. This means for variables declared in another module, your module gets a new variable of the same name. The second thing to note is that assignment only affects the variable assigned to.

Your code should look like this:

first.py:

import third
from second import *

while third.running:   # This now accesses the running variable in third
    off()

second.py:

import third

def off():
    third.running = False   # This now reassigns the running variable in third
Sign up to request clarification or add additional context in comments.

Comments

0

In third.py,
Make A Getter Function For running Variable.
It Will Look Like:

running = True
def getRunning():
    return running

And In first.py,
It Will Look Like:

while getRunning():
    off()

2 Comments

The problem still lies in second.py. The assignment there only affects that variable in that module and has no effect on the one in third.py
Just Like Getter, Add One Setter To It

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.