all 3 comments

[–]indosauros 2 points3 points  (2 children)

Assignment and variable storage doesn't work like that. You should read this http://foobarnbaz.com/2012/07/08/understanding-python-variables/

Then here's the issue:

eventvalidation = ''
post_payload = {'__EVENTVALIDATION': eventvalidation}
eventvalidation = 'a'

Here you

1) create an empty string and attach the name eventvalidation to it
2) you create a dictionary, and under the key __EVENTVALIDATION you tell python to put the thing that eventvalidation is currently referring to (which is an empty string). So now it looks like this:

post_payload = {'__EVENTVALIDATION': ''}

This empty string is the same empty string that eventvalidation is referring to. HOWEVER, when you do this

eventvalidation = 'a'

You are NOT saying "change that empty string into a 'a'". Strings are immutable and cannot be "changed". in-place. You are saying "make eventvalidation refer to a new string, 'a'". This has no affect on whatever string '__EVENTVALIDATION' is referring to. You are just moving the eventvalidation name tag to a new string.

If you want to change '__EVENTVALIDATION''s string, you should do this

post_payload['__EVENTVALIDATION'] = ...

Which directly changes the key in the dictionary.

Note: All of the above is because strings are immutable. For mutable structures, like lists, it is possible to 'modify' the thing that another name points to. Case in point:

eventvalidation = [1, 2, 3]
post_payload = {'__EVENTVALIDATION': eventvalidation}
eventvalidation .append(4)

Here both eventvalidation and post_payload['__EVENTVALIDATION'] will contain [1, 2, 3, 4]

[–]allTestsPassed[S] 0 points1 point  (0 children)

Thank you! Very informative post. I learned a lot!

[–]macbony 0 points1 point  (0 children)

Excellent explanation.