简体   繁体   中英

How to pass list from one function to another without using global variable?

I am quite new to python and currently trying to work on a project that asks the user to read data from a csv file, and to store that data so that other functions can be performed on it. The only issue I seem to be having at the moment is that I can't use global variables .

Just now I have the following code structure:

import csv
import sys
data_set = []

def loadFile(x):
    with open(x, "r") as readfile:
        csv_reader = csv.reader(readfile, delimiter= ';')
        for row in csv_reader:
            data_set.append(row)
    print("Loaded weather data from", (x[0:-4]).capitalize())
    print()

def avgDay(x):
    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
           print("The weather on", x, "was on average", line[2], "centigrade")

Is there some way I can call data_set to other functions? (I have a few more functions that need to work with the data).

Yes, simply pass it in as a parameter, and return it when it is originally generated.

import csv
import sys

def loadFile(x):
    date_set = []
    with open(x, "r") as readfile:
        csv_reader = csv.reader(readfile, delimiter= ';')
        for row in csv_reader:
            data_set.append(row)
    print("Loaded weather data from", (x[0:-4]).capitalize())
    print()
    return data_set

def avgDay(x, data_set):
    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
           print("The weather on", x, "was on average", line[2], "centigrade")

def main():
    data_set = loadFile(...)
    avgDay(..., data_set)

if __name__ == 'main':
    main()

Your loadFile function can return a dataset that can be used inside avgDay

import csv
import sys


def loadFile(x):

    data_set = []

    with open(x, "r") as readfile:
        csv_reader = csv.reader(readfile, delimiter= ';')
        for row in csv_reader:
            data_set.append(row)
    print("Loaded weather data from", (x[0:-4]).capitalize())
    print()

    return data_set

def avgDay(x):

    data_set = loadFile(x)

    for line in data_set:
        if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
           print("The weather on", x, "was on average", line[2], "centigrade")

global vars can only be set if u use the word global

you can access global vars without anything special

data_set = "global_var" #global

def print_global_data_set():
    print(data_set)

def set_global_data_set():
    global data_set # you need this line to tell python that you are going to set a global variable
    data_set = 'global_var_set_from_function'

def set_local_data_set():
    data_set = "local" # this will not set the global var but a local var so the global data_set stays unchanged

print(data_set) # outputs "global_var"
set_local_data_set()
print_global_data_set() # outputs "global_var" (set_local_data_set did not set the global var but a local var)
set_global_data_set()
print_global_data_set() # outputs "global_var_set_from_function"

Functions can have attributes. You can make data_set an attribute of loadFile and then reference it in avgDay.

with open(x, "r") as readfile:

    csv_reader = csv.reader(readfile, delimiter= ';')

    for row in csv_reader:

        data_set.append(row)

    loadFile.data_set_attr = data_set

Then, in avgDay, reference loadFile.data_set_attr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM