I'm having a really bizarre issue in a program I'm writing. I'm trying to find an object in a dictionary, and if the name string in the object matches the name I'm looking for, then I append a message to the empty list. Afterwards, I would expect to have a list of a single string. What I'm actually getting though is quite surprising to me, and I can't for the life of me explain it.
clients = {}
class Client:
my_fullname = ""
mymessage = []
def send_message(recipient, message, clients):
for guy in clients:
if clients[guy].my_fullname == recipient:
print(clients[guy].my_fullname)
print(clients[guy].mymessage)
clients[guy].mymessage.append(message)
return
SCOTT = Client()
SCOTT.my_fullname = "SCOTT"
KAT = Client()
KAT.my_fullname = "KAT"
clients["SCOTT"] = SCOTT
clients["KAT"] = KAT
print(clients["KAT"].mymessage)
send_message("KAT", "goodbye", clients)
print(clients["KAT"].mymessage)
print(clients["SCOTT"].mymessage)
send_message("SCOTT", "what's up", clients)
print(clients["SCOTT"].mymessage)
And here's the output I'm getting from running that. I'm on Python 2.7.5. I've tried having clients passed in as a parameter, I've tried having it as a global without passing it in, and I've tried having it as a global and not passing it in but declaring "global clients" at the beginning of the function and I get the same result regardless. Help please?
[]
KAT
[]
['goodbye']
['goodbye']
SCOTT
['goodbye']
['goodbye', "what's up"]