[Tutor] Problems with deleting dictionary keys.
Allan Crooks
allan.crooks@btinternet.com
Mon, 13 Aug 2001 19:16:55 +0100
Hi,
I've got the following problem with dictionaries. The problem is, I get
this error when I try to delete a key value with the __setitem__
method.
I feel that the problem will be clearer if I give you example code. :)
-------
Python 2.2a1 (#21, Jul 18 2001, 04:25:46) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
>>> class d2(dictionary):
... def __setitem__(self, key, value):
... if value==0:
... del self[key]
... else:
... print "Setting [%s] to %s." % (key, value)
...
>>> d = d2()
>>> d.update({'p': 1, 'l': 2, 'a': 3, 'i': 4, 'd': 5})
>>> d
{'a': 3, 'p': 1, 'i': 4, 'd': 5, 'l': 2}
>>> d['a']=10
Setting [a] to 10.
>>> d['a']=0
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 4, in __setitem__
SystemError: NULL object passed to Py_BuildValue
I seem to remember there being a problem in earlier versions of
Python, where if you modify the dictionary while doing other things
to it, it would crash the interpreter.
So what I need to know is if this is a bug, or if I'm doing something
wrong....
Oh, in case you're wondering what this is for, it's part of a data
structure which is useful for seeing if you can derive certain words
from a set of letters.... Well, it can do a few more things than that,
but that's the gist of it. :)
If anyone's interested in it, I'll post it when I get it working. But at
the moment, this is the only thing holding me back. :(
Thanks,
Allan.