简体   繁体   中英

Null a dictionary inside a function in python

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.

When you say

a = None

you are making a refer to None , which was earlier pointing to the dictionary object. But when you do

a['car'] = 9

a is still a reference to the dictionary object and so, you are actually adding a new key car to the dictionary object only. That is why it works.

So, the correct way to clear the dictionary is to use dict.clear method, like this

a.clear()

the thing why its not working:

>>> a=[1,2,3,4]
>>> def destroy(a):
...     del a          # here a is local to destroy only
... 
>>> destroy(a)
>>> a
[1, 2, 3, 4]

You're not destroying anything, you're just changing a 's value to None in destroy 's local scope.

Why are you not simply using:

some_dictionary = None

To remove reference to some_dictionary ? The garbage collector will do the destruction when no one references some_dictionary anymore.

In your code:

b = None # replaced: destroy(b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM