UserDict and items()

Alex Martelli aleaxit at yahoo.com
Fri Mar 9 08:57:34 EST 2001


"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn9aed8i.cg1.scarblac at pino.selwerd.nl...
    [snip]
> The cuteness introduced in later Python versions would allow that to be:
>
>    def __getitem__(self, key):
>       return self.data.setdefault(key, DatabaseCall(key))
>
> Ie, look up everything in the database that's not in the dictionary yet,
put
> it in, and return what's in the dict.

Unfortunately, this approach is not lazy -- DatabaseCall is here
being called also for keys which ARE already among those of self.data.
A more explicit expression is needed to allow the laziness.


Alex






More information about the Python-list mailing list