0

For example, let's say I have this "pseudo" code here:

def on_start():
    members = {'guy1':'1234','guy2':'5678'}

def function1():
    # we do something with the dict, let's say, print each member
    print(members.keys())

# ex: new_member = {'guy3':'9012'}
def function2(new_member):
    # something happened so the dict now has a new member
    members.update(new_member)

on_start()
while True:
    function1() # run on a separate thread
    # the condition would be "if I triggered the "add_member" event, do function2
    if condition:
        function2(input())

I basically have a function that is called upon the script's startup, which initializes the dictionary, and then there's function1() that loops and uses that dictionary, and if a new member is added from user input, then function2() is called which should add the member to the dict, so the next time function1() is called, it will find that newly added member.

How can I achieve this ?

TermoZour
  • 157
  • 8
  • First of all, do not use names of built-ins, such as `dict` to name your variables. Second, your `on_start()` function keeps the generated dictionary in its scope so other functions cannot modify it - declare it as `global` first. Third, you have to define what `condition` is, and lastly, you need to deal with potential concurrency issues if you're going to run the functions in different threads. – zwer Apr 04 '18 at 21:27
  • I've edited the condition, and the variable name – TermoZour Apr 04 '18 at 21:32
  • What concurrency issues ? – TermoZour Apr 04 '18 at 21:32

1 Answers1

0

The issue is your 'dict' variable wont be accessible outside of the scope of your on_start() function. I was able to get the result you wanted with a couple small tweaks to your code:

def on_start():
    return {'guy1':'1234','guy2':'5678'}

def function1(dict):
    # we do something with the dict, let's say, print each member
    for key, value in dict.items():
        print (key, value)

# ex: new_member = {'guy3':'9012'}
def function2(new_member):
    # something happened so the dict now has a new member
    dict.update(new_member)

dict = on_start()
while True:
    function1(dict) # run on a separate thread
    name = raw_input("What is your name? ")
    id = raw_input("What is your id? ")

    if name and id:
        function2({name: id})

To summarize i call on_start to initialize and return your dict.

The code then enters your while loop where function1 is called. If user input is taken sucessfully (im not doing any input validation, ill let you do that) Function 2 is called to update the dictionary. Then this process repeats

Also recommend you change the name of 'dict' to a non built-in name as @zwer suggested.

I dont think there is a need to spawn another thread here, this will likely be problematic since you would be reading/writing the same variable concurrently.

DannyMoshe
  • 6,023
  • 4
  • 31
  • 53
  • I've seen there's "global variables". Why didn't you use something like that instead of your current answer ? – TermoZour Apr 04 '18 at 22:05
  • I generally try to avoid using global variables at all costs (unless to define constants) .. see this - https://stackoverflow.com/questions/19158339/why-are-global-variables-evil – DannyMoshe Apr 04 '18 at 22:09