Re: Some things I'd like

Marc Wachowitz (mw@ipx2.rz.uni-mannheim.de)
10 Aug 1994 08:04:38 GMT

Craig Lawson (claw@rahul.net) wrote:
> Yesterday, I needed to duplicate a dictionary. new_dict = dict[:]
> won't do it. Is there a good way to do this?

new_dict = {}
for (key, value) in dict.items():
new_dict[key] = value

Of course, you can put this in a separate function if you need it often.
However, I think it would really be nice if one could loop over elements
of a mapping without having to construct a sequence first. One way to do
it (with modification of Python's runtime system) would be to make "for"
call the iteration object with a method __cursor__(), and then call what
this method returned with __item__(n) for all numbers from zero upwards.
The end could be signalled by an exception. Objects which already have a
__item__ method (like sequences) would work by just returning themselves
as __cursor__(), whereas other iterations could easily be programmed, by
providing a suitable cursor which manages whatever internal state it may
need to walk over the original object. Allocating everything as a list -
for example a big database - isn't a very attractive solution, IMO.

------------------------------------------------------------------------------
* wonder everyday * nothing in particular * all is special *
Marc Wachowitz <mw@ipx2.rz.uni-mannheim.de>