Apologies for the slightly vague question, but I'm not too sure how to word it.
The following very basic code example isn't doing what I'd expect it do, and I'd like to understand why...
class Message():
def __init__(self, data={}):
print(f"construct with {data}")
self._data = data
def send_a_message():
msg = Message()
print(msg._data)
msg._data['key'] = "value"
if __name__ == '__main__':
send_a_message()
send_a_message()
The output is as follows:
construct with {}
{}
construct with {'key': 'value'}
{'key': 'value'}
So, the 2nd time a new Message object is constructed, the previous dict object is being passed to the constructor.
In both cases, Message() isn't passed an arg, so I'd expect it to be constructed with data={}
- a new empty dict each time. I can't get my head around how it could possibly be remembering the previous one. This really doesn't make sense to me, so I'd be very grateful for an explanation.
Thanks!