I'm trying to import a class from another file and then implement the member function in my main function. I'm really just trying to understand the syntax of Python, as I am still really new to the language. My program is simple, and isn't really meant to do much. I'm more or less just trying to get a grasp on how Python goes about this. My class file is called Parser.py and here's is the code:
class Parser:
def hasMoreCommands(self):
if not c:
return false
else:
return true
and my main function is in a file called jacklex.py The main function only opens an input file and copies the text to an output file. Here's the code:
import Parser
from Parser import *
f = open('/Python27/JackLex.txt' , 'r+')
fout = open('/Python27/output.txt' , 'w')
while Parser.hasMoreCommands:
c = f.read(1)
fout.write(c)
print "All Done"
f.close()
fout.close()
My issue is that my program runs, but it seems to be getting stuck in an infinite loop. There's never any text printed to the ouput file, and "All Done" is never printed in the Python Shell. Am I missing something essential that's causing my program not to work properly?
Parser.hasMoreCommandsmethod is trying to access a variable namedc, but there's no local or global with that name. The fact thatjacklex.pyhas a global with the same name won't help you. If you wantParserto see it, you need to pass it over. For example,def hasMoreCommands(self, c):, thenwhile parser.hasMoreCommands(c):cI would want it inside the loop, but when I putc = f.read(1)inside my loop I get an error saying "name 'c' is not defined' and it's referring to the linewhile parser.hasMoreCommands(c)How would I then get past this error?