0
def part1():
with open("test.txt") as file:
    data = csv.reader(file, delimiter=',')
    for row in data:
        a = row[0]
        b = row[1]
        c = row[2]
        d = row[3]
        e = row[4]
        f = row[5]
def printing():
    print (a)

So I just want to use the variables from "part1" in "printing". I have tried stating that each variable in part1 as "global repetitive and I feel like there's a much more efficient way to achieve this.

Any help would be appreciated. (and please don't judge my code, it's just an example)

  • I'd say either change the scope to global or return the variable which you'd like to print and print the function. – Mathieu Brouwers Jan 26 '16 at 10:38
  • I want to be able to use every single variable in part1 in multiple other functions. Also, what do you mean by "change the scope to global"? :o – Beast Mode Jan 26 '16 at 10:39
  • I mean you could declare the variable outside of the function. This answer seems like it's the same as what you are looking for. http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them But with the amount of variables, it doesn't seem like a good solution to declare all of the variables in the global scope – Mathieu Brouwers Jan 26 '16 at 10:41
  • Ok, thank you for your help. – Beast Mode Jan 26 '16 at 10:43
  • For example create list outside of the function as global and then just append it instead of creating new variables. Then you will be able to access variables by index in there for printing. – T.Chmelevskij Jan 26 '16 at 10:44

0 Answers0