dict.updated

R

Rick Morrison

Would there be any way to add a method to all dict objects that operated
like the .update() method, but also returned a reference to the updated
dict?

..update() is clumsy to use inside of list comprehensions and the like. Or am
I missing something?

Thanks,
Rick
 
S

Steven Bethard

Rick said:
Would there be any way to add a method to all dict objects that operated
like the .update() method, but also returned a reference to the updated
dict?

Are you looking for updated() to parallel sorted(), where sorted()
returns a *new* list? I doubt you'll be able to rally much support for
a method that changes an object and returns a reference to it -- check
the archives to see these kind of things getting rejected over and over.

Could you do something like:

py> import itertools
py> d1 = dict(a=1, b=2)
py> d2 = dict(c=3, d=4)
py> d3 = dict(itertools.chain(d1.iteritems(), d2.iteritems()))
py> d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}

Steve
 
R

Rick Morrison

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:
.... 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.

-- Or maybe I'm just lazy and picked up too many bad habits from
Javascript.

Rick
 
S

Stephen Thorne

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:
... 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.
 
S

Steven Bethard

Rick said:
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:


... 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.

You could do something like:

py> class dict(dict):
.... def updated(self, *args, **kwds):
.... self.update(*args, **kwds)
.... return self
....
py> [d.updated(c=3) for d in [dict(a=1, b=2), dict(x=10, y=11)]]
[{'a': 1, 'c': 3, 'b': 2}, {'y': 11, 'x': 10, 'c': 3}]

It'd mean you'd have to create all your dicts with the dict constructor
instead of {} though.

Steve
 
H

hanz

Rick said:
[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}]

I don't really understand the use of this. Can you give a less toy
example? I'd probably just do

dicts = [{'a':1, 'b':2}, {'x':10, 'y':'11'}]
for d in dicts: d.update({'c':3})

This isn't really more typing or any clumsier IMO, so I can't see why
you'd want to use list comprehensions in this case.
 
B

Bengt Richter

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:
... 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}]
It's kind of a strange list comp, but I guess you're just illustrating something.
But for this case you could just write
>>> [d.update({'c':3}) or d for d in [{'a':1, 'b':2}, {'x':10, 'y':'11'}]]
[{'a': 1, 'c': 3, 'b': 2}, {'y': '11', 'x': 10, 'c': 3}]

taking advantage of normal d.update() returning None and so forcing the 'or' term.


Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,716
Latest member
MiloManley

Latest Threads

Top