dict.updated
Stephen Thorne
stephen.thorne at gmail.com
Wed Jan 12 19:01:33 EST 2005
On Wed, 12 Jan 2005 23:47:13 GMT, Rick Morrison
<ram at forefront-tech.no.lunch.meat.com> wrote:
> I could live with creating a new dict, sure (although it seems wasteful). I
> realize that something like this probably doesn't stand a chance of ever
> making it into the std library for what might be called "philosophical"
> reasons. I just want it for me (my personal philosophy runs more to the
> pragmatic -- well at least for coding).
>
> I suppose the in-place version would be more along the lines of:
>
> >>> def updated(d, updates):
> ... d.update(updates)
> ... return d
> ...
> >>> [updated(d, {'c':3}) for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]]
> [{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]
>
> But I'd like to put the "updated" method on the "dict" object, which is what
> I can't seem to figure out.
> Yeah I know that's "bad", but to my mind so is polluting the global
> namespace with the "updated" function.
[dict(d.items() + {'c':3}.items()) for d in [{'a':1, 'b':2}, {'x':10,
'y':'11'}]]
seems logical enough to me....
> -- Or maybe I'm just lazy and picked up too many bad habits from
> Javascript.
probably.
Stephen.
More information about the Python-list
mailing list