1

I have a function that builds a list from a very large file. That list is used several times at various stages. Obviously, it only needs to runs once and running it more would waste time.

My question is how to have a function run once and return the list but when that function is called again it simply returns the list and doesn't run again.

I'd rather not just make the list a global variable. I know I could use a while loop to remember it as well but that just seems wasteful. I assume there is a more elegant way of accomplishing what I'm looking for.

Am I looking to use decorators or singletons (not sure if they are in python).

Example:

state_ab = ['OH', "test", 'CO']


def build_US_state_list():
    state_dict = []
    for state in pycountry.subdivisions:
        if state.country_code == 'US':
            state_dict.append({'name': state.name, 'code': state.code[3:]})
    return state_dict


def get_US_state_list():
    try:
        return state_dict
    except:
        state_dict = build_US_state_list()
        print('ran')
        return state_dict


def identify_and_add_US_states():
    states = get_US_state_list()
    print(states)
    for i in range(0, len(state_ab)):
        for state in states:
            if state_ab[i] == state["code"]:
                return state["code"]
  • Please post your code so that we can see what you are trying to accomplish and what your specific problem is. That is how questions have to be structured on this site. We have to see an attempt. – Simeon Ikudabo Jun 03 '18 at 03:23
  • @HunterForce, state_dict must be a global variable, or if you could make a class, then using self.state_dict will help too. This way, once initialized, you won't have to call "build_state" function again. – farazmateen Jun 03 '18 at 03:35
  • @farazmateen Thanks for the answer! I knew that was a solution but I was hoping there would be a simpler/more elegant solution. – HunterForce Jun 03 '18 at 03:37
  • @HunterForce Oh sorry, missed that part in your post :) – farazmateen Jun 03 '18 at 03:39
  • @Stephen Rauch I appreciate that you are trying to organize posts on this site but I wouldn't have asked if I didn't know the answer. I also clearly couldn't find where this had been asked before. If I had I would have just read that. In the future please at least point me in the right direction to the previously asked post. Just making as duplicate and leaving is why this site can be so inhospitable to beginners trying to learn. – HunterForce Jun 03 '18 at 03:41
  • @farazmateen not a problem! Thanks for the help! – HunterForce Jun 03 '18 at 03:42
  • 1
    @HunterForce, What? Marking as duplicate is how we *point you at previously asked posts*. I love to learn, so please explain how exactly, in your highly experienced opinion, should I have done this differently? – Stephen Rauch Jun 03 '18 at 03:44
  • @Stephen Rauch Marking it as a duplicate does just that. It says "Hey, somewhere on this vast site there is already an answer here (which is probably true for almost all questions at this point)." It doesn't help newbies find that post or even help give us clues on what to search to find that post. The best possible way to help is to walk me through my problem but thats unreasonable. All most newbies want is to know the correct terms to google. Thats the problem with self learning. I don't know what I don't know. I often couldnt find the correct terms to google.. – HunterForce Jun 03 '18 at 06:26
  • @HunterForce Thank you for the clarification. Now I understand the problem. You mistakenly feel this site exists to help you (the OP, the person asking the question). In fact, the site's purpose is to help the next person. Those who find this question through a search engine. The fact that most of the time we also directly help the OP, is a side-effect. I am truly sorry that you find it inhospitable that a complete stranger pointed you to the information you need to answer your question less than 6 minutes after you asked it. It must be especially egregious that the stranger did this for free. – Stephen Rauch Jun 03 '18 at 13:03
  • @Stephen Rauch If a persons search popped up my question because they too did not know the correct terms to google, and all they saw was that it was a duplicate post without anything else (a link to the original question or even an actual answer) how would that be helpful? As for the complete stranger who helped me. He's fantastic! I wish I could do more than thank him. When I understand programming better I hope to be helpful like him! I just wonder how many other helpful people saw "duplicate" and didn't bother to click. I mean, marking as duplicate and saying "memoization" would've worked. – HunterForce Jun 03 '18 at 19:28
  • @HunterForce, but that is in fact the entire point of the duplicate mark. It contains a link to the duplicate. If the reader can't be *bothered*, why is the duplicate marker expected to do more work on their behalf? Click the link in the duplicate message. Read what's there. Life is good. Cheers. – Stephen Rauch Jun 03 '18 at 19:31

0 Answers0