I'd like to figure out how I should use a class to read input from a file so that I can use that data in other classes. If I read input from a file into a list, should I pass that to another class that needs that to use that information?
Right now I have:
import sys
class FileReader:
    """Reads a file"""
    def __init__(self):
        input = ''
        try:
            with open(sys.argv[1], 'r') as inFile:
                input = inFile.readline()
                print(input)
        except IndexError:
            print("Error - Please specify an input file.")
            sys.exit(2)
def main():
    x = FileReader()
if __name__ == "__main__":
    main()
I thought about making some kind of list to hold strings from the file, but I'm not sure whether that should be global or not.
__init__and pass insys.argv[1]or a file path obtained however else you like when creating an instance ofFileReadersys.exit()- let the calling code catch the exception and decide what to do. You can re-raise with a more informative exception rather than printing an error message (not sure what's the norm here though)argparseand validation should occur there... then the classes work on data passed to them from that... It means that your classes are more usable for data from arbitary sources (ie: you want to take a filename from a database result) - at the basic level -def(a, b)shouldn't care whereaandbcome from as long as it can work them.