0

I'm storing a list in a class in python, but when I later try to access it it's empty. Here is the relevant code and output.

In compiler.py

# globals
verbose = True
current_context = ""

def compile(tokens):
    global verbose, current_context

    if verbose: print("verbose output enabled.")

    # first we want to find all the sections.
    firstpass_token_id = 0
    gathered_tokens = []
    gather_tokens = False
    for t in tokens:
        if gather_tokens:
            gathered_tokens.append(t)

        if t == SECTION:
            current_context = extract_section_name(tokens[firstpass_token_id + 1])
            gather_tokens = True
            if verbose: print("gathering tokens for section", current_context)

        if t == END:
            add_section(current_context, gathered_tokens)
            current_context = ""

            gathered_tokens.clear()
            gather_tokens = False

        firstpass_token_id += 1

    # then we want to call the main section
    call_section("main")

def call_section(name):
    global verbose, current_context

    if verbose: print("calling section", name)

    skip_until_next_end_token = False 

    tokens = get_tokens(name)
    print(len(tokens))

In sections.py

sections = []
section_id = 0

class Section:
    def __init__(self, id, name, tokens):
        self.id = id
        self.name = name

        print("storing the following tokens in", name)
        print(tokens)
        self.tokens = tokens

    def get_tokens(self):
        print("retrieving the following tokens from", self.name)
        print(self.tokens)
        return self.tokens

def add_section(name, tokens):
    global sections, section_id
    sections.append(Section(section_id, name, tokens))
    section_id += 1


def get_tokens(name):
   global sections
   for s in sections:
       if s.name == name:
           return s.get_tokens()

Output

verbose output enabled.
gathering tokens for section foo
storing the following tokens in foo
['foo()', 'var', 'lmao', '=', '2', 'end']
gathering tokens for section main
storing the following tokens in main
['main()', 'var', 'i', '=', '0', 'i', '=', '5', 'var', 'name', '=', '"Douglas"', 'var', 'sentence', '=', '"This is a sentence."', 'foo()', 'end']
calling section main
retrieving the following tokens from main
[]
0

Note that these aren't the complete files, let me know if you need to see more of the code. I'm sure it's something stupid, I haven't worked with python for a couple of months. Thanks.

1 Answer 1

1

You are passing in gathered_tokens, so your Section object has self.tokens = tokens.

In other words, gathered_tokens and self.tokens now point to the same list.

Then you call gathered_tokens.clear().

Now they STILL point to the same list. It's an empty list.

Try self.tokens = tokens[:] to make a copy.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.