Turning a tuple inside a function into a global value

Viewed 579

I am trying to turn the text of a text file into a tuple. I'm a Python beginner and assume this code will be not as straight forward as it could be and thats okay with me (for now). All I am trying to do is making the tuple numbers_bank a global value so I can access it outside the function.

Printing the tuple from inside the function works but it doesn't work outside the function.

Can someone tell me what I am doing wrong? (It is a requirement that the output is a tuple, otherwise I would have changed it to a list and just append the elements.)

global numbers_bank

def read_numbers():

    file = open("numbers.txt")
    num = file.read().split(",")
    num_new = [s.replace("[", "") for s in num]
    numbers_bank = [a.replace("]", "") for a in num_new]
    tuple(numbers_bank)

read_numbers()
print(numbers_bank)
2 Answers

This is how the global keyword works: https://www.programiz.com/python-programming/global-keyword

in your case it would be:

numbers_bank = None

def read_numbers():
    global numbers_bank

    file = open("numbers.txt")
    num = file.read().split(",")
    num_new = [s.replace("[", "") for s in num]
    numbers_bank = [a.replace("]", "") for a in num_new]
    tuple(numbers_bank)  # Note: this doesn't do anything

read_numbers()
print(numbers_bank)

However, global variables should be avoided. Instead you should make the function return numbers bank in the following way:

def read_numbers():
    file = open("numbers.txt")
    num = file.read().split(",")
    num_new = [s.replace("[", "") for s in num]
    numbers_bank = [a.replace("]", "") for a in num_new]
    return tuple(numbers_bank)  # Now the 'tuplified' version of numbers_bank will be used for something because it is returned by the function

numbers_bank = read_numbers()
print(numbers_bank)

Please also note that the 'tuple(numbers_bank)' line doesn't do anything because you haven't assigned it to any variable.

What you ask for:

>>> def read_numbers(): 
...:     global numbers_bank 
...:     # do stuff 
...:     numbers_bank = [1, 2, 3] # create some list
...:     numbers_bank = tuple(numbers_bank)                                                                                                                   

>>> read_numbers()                                                                                                                              
>>> numbers_bank                                                                                                                               
(1, 2, 3)

What you should do instead:

>>> def read_numbers(): 
...:     # do stuff
...:     numbers_bank = [1, 2, 3] # create some list
...:     numbers_bank = tuple(numbers_bank) 
...:     return numbers_bank 

>>> numbers_bank = read_numbers()                                                                                                              
>>> numbers_bank                                                                                                                               
(1, 2, 3)

Changing global state within a function can become messy and hard to comprehend very quickly. It's cleaner to return the value you are computing from a function and then explicitly assign a name to that return value whenever possible.

Also note that it's a little more concise to use tuple(generator-expression) over using a list comprehension and then applying the tuple constructor to the result of the comprehension, i.e.

numbers_bank = tuple(a.replace("]", "") for a in num_new)
Related