I'm not quite sure why the following doesn't work. I tried to send a dictionary object to a function, test a few things and make the dict null if certain criteria was met. I don't know why.
A simple version of what I tried to do:
def destroy_bad_variables(a):
# test if a is a bad variable, nullify if true
#del a # doesn't work.
a = None
print a # this says its null
def change(a):
a['car'] = 9
b = {'bar':3, 'foo':8}
print b
change(b)
print "change(b):", b
destroy_bad_variables(b)
print "destroy_bad_variables(b):", b
It produces the following output:
{'foo': 8, 'bar': 3}
change(b): {'car': 9, 'foo': 8, 'bar': 3}
None
destroy_bad_variables(b): {'car': 9, 'foo': 8, 'bar': 3}
The dict can be modified by a function as is expected, but for some reason it can't be set to None. Why is this? Is there some good reason for this seemingly inconsistent behaviour? Forgive my ignorance, none of the books I've read on python explain this. As I understand it dicts are "mutable", the function should null the dict object and not some copy of it.
I know I can work around it by setting b = destroy(b) and returning None from destroy(), but I don't understand why the above doesn't work.
a's reference toNoneindestroy's local scope.