Passing by reference

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Dec 22 08:20:32 EST 2007


On Sat, 22 Dec 2007 04:13:31 -0800, MartinRinehart wrote:

> Bruno Desthuilliers wrote:
>> ...  that's definitively not
>> something I'd store in global.
> 
> So where would you put it?

Context is all gone, so I'm not sure that I remember what "it" is. I 
think it is the text that you're parsing.

I believe you are currently doing something like this:

TEXT = "placeholder"

def parse():
    while True:
        token = get_next_token() # looks at global TEXT
        yield token

# And finally actually run your parser:
TEXT = open("filename", "r").read()
for token in parse():
    print token



If I were doing this, I would do something like this:

def parse(text):
    while True:
        token = get_next_token() # looks at local text
        yield token

# Run as many independent parsers as I need:
parser1 = parse(open("filename", "r").read())
parser2 = parse(open("filename2", "r").read())
parser3 = parse("some text")

for token in parser1:
    print token
# etc.



Unless the text you are parsing is truly enormous (multiple hundreds of 
megabytes) you are unlikely to run into memory problems. And you gain the 
ability to run multiple parsers at once.



-- 
Steven



More information about the Python-list mailing list