On 10/22/2010 6:10 AM, Ethan Furman wrote:
John Nagle wrote:
class nonnulldict(dict) :
def __setitem__(self, k, v) :
if not (v is None) :
dict.__setitem__(self, k, v)
That creates a subclass of "dict" which ignores stores of None values.
So you never store the unwanted items at all.
It's going to take more work than that...
--> nnd = nonnulldict(spam='eggs', ham=None, parrot=1)
--> nnd
{'ham': None, 'parrot': 1, 'spam': 'eggs'}
--> d['more_hame'] = None
--> nnd.update(d)
--> nnd
{10000:True, 'more_hame':None, 'ham':None, 'parrot':1, 'spam':'eggs'}
You're right.
class nonnulldict(dict) :
def __init__(self, **args) :
dict.__init__(self)
for k in args :
self.__setitem__(k, args[k])
def __setitem__(self, k, v) :
if not (v is None) :
dict.__setitem__(self, k, v)
There should also be a copy constructor, so you can convert a dict to a
nonnulldict, a list or tuple of pairs to a nonnulldict, etc.
But at least this will raise an exception for those cases.
John Nagle
--
http://mail.python.org/mailman/listinfo/python-list