Python: interpreter says "None" after parsing file
Question: I've written a simple interpreter with Python for a programming language I've
created. I have a little problem though: when I try to read a file, so the
code can be interpreted, Python says `None`.

Here's the interpreter code:

    
    
    import sys
    
    class Interpret:
        def read(self, cmd):
            tokens = cmd.split("\n")
            for i in range(0, len(tokens)):
                self.parse(tokens[i])
    
        def parse(self, cmd):
            if cmd.startswith("print(") and cmd.endswith(")"):
                cmd = cmd[6:]
                cmd = cmd[:-1]
                return(cmd)
    
    interpret = Interpret()
    
    code = open(sys.argv[1], 'r')
    print(interpret.read(str(code)))
    

Here's the code in the file I want to read: `print(Hi)\n` (Yes, there's a
newline there, not the characters `\n`)

So, here's what happened in CMD:

    
    
    > python interpreter.py test.tl
    
    None
    

It should output `Hi`, but it doesn't work properly. I think it has something
to do with the last two lines of the interpreter code, but I'm not completely
sure.


Answer: You're just _calling_ `self.parse` and not using its return value and, given
that `self.parse` doesn't output anything and `read` returns nothing (`None`),
you're getting `None` as a result.

You may want your `read` to `yield self.parse(tokens[i])` on each iteration,
thus making it a generator, and then do something like this:

    
    
    for data in interpret.read(str(code)):
        print(data)
    

