Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to programming with python and I have this problem. I assigned global string with value, and after that in other file try to read it but the value of global string was empty.
Here is my code:

file1.py
string_global = ''
#if use list - string_global[] the problem is solved but why? 


file2.py
from file1 import string_global

def func_local():
    global string_global
    string_global = "here i am"
    print(string_global)



file3.py
from file1 import string_global

def func_another_local():
    global string_global
    print("Are you still here? ",string_global)



file4.py
from file2 import func_local
from file3 import func_another_local

def start():
    func_local()
    func_another_local()

start()


What I have tried:

I found work around when assign a list, not a string.. but I don't feel happy to don't know the answer of such a simple case in programing.
I thing the problem is that the script where global variable is stored is interpreted once again when calling file3 func_another_local. But why when I use list this does not happen.. What is the correct way of using the global variables in python, because the chain imports does not seems correct to me..
Is this the correct way :
Programming FAQ — Python 3.11.1 documentation[^]
Posted
Updated 11-Dec-22 4:21am
v3

1 solution

In your code, you are trying to use a global variable defined in one file in another file, but you are not using the global keyword in the correct way. In Python, you must use the global keyword to declare that a variable is global, and you must use it in the same scope where you want to use the global variable.

In your code, you are defining the string_global variable in file1.py as a global variable, but you are not using the global keyword in the correct way in file2.py and file3.py. Instead, you are declaring a new local variable with the same name as the global variable, which shadows the global variable and prevents you from accessing its value.

To fix this issue, you should use the global keyword in the correct way in file2.py and file3.py. Here's an example of how you might do this:

# file2.py
from file1 import string_global

def func_local():
    # Declare that the string_global variable is global
    global string_global
    string_global = "here i am"
    print(string_global)


# file3.py
from file1 import string_global

def func_another_local():
    # Declare that the string_global variable is global
    global string_global
    print("Are you still here? ",string_global)


By using the global keyword in this way, you can access the global string_global variable from file1.py in file2.py and file3.py.

Keep in mind that using global variables in this way is not always the best approach, and you should consider other options, such as passing the value of the global variable as an argument to the functions that need it, or using a class to manage the global state of your program.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900