[Python-Dev] dict.supplement() (was Re: list.shift())

artcom0!pf@artcom-gmbh.de artcom0!pf@artcom-gmbh.de
Fri, 17 Mar 2000 23:43:35 +0100 (MET)

Ka-Ping Yee wrote:
> >     # pretend lists are implemented in Python and 'self' is a list
> >     def shift(self):
> >         item = self[0]
> >         del self[:1]
> >         return item

Guido van Rossum:
> You can do this using list.pop(0).  I don't think the name "shift" is very
> intuitive (smells of sh and Perl :-).  Do we need a new function?

I think no.  But what about this one?:

	# pretend self and dict are dictionaries:
	def supplement(self, dict):
	    for k, v in dict.items():
	        if not self.data.has_key(k):
		    self.data[k] = v

Note the similarities to {}.update(dict), but update replaces existing
entries in self, which is sometimes not desired.  I know, that supplement
can also simulated with:
	tmp = dict.copy()
	self.data = d
But this is stll a little ugly.  IMO a builtin method to supplement
(complete?) a dictionary with default values from another dictionary 
would sometimes be a useful tool.

Regards, Peter
Peter Funk, Oldenburger Str.86, D-27777 Ganderkesee, Germany, Fax:+49 4222950260
office: +49 421 20419-0 (ArtCom GmbH, Grazer Str.8, D-28359 Bremen)