UserDict and items()

Oliver Hofmann a2619725 at smail.uni-koeln.de
Thu Mar 8 17:28:52 EST 2001


Thanks Steve, Remco!


Looks like I should have been more precise or given a better description.
As it is a lot of problems mentioned here do not show up in the actual
application.

>> class ClText(UserDict):
>>     def __init__(self):
>>         UserDict.__init__(self)
>>
>>         # Set initial value
>>         self.data['test'] = ''
>>
>Since __getitem__() is checking for this value, why bother to add it to the
>dictionary?

The dictionary is supposed to contain information from different tables of
a database. When it is initialized I set various keys with empty strings as
values since this allows a user to look at allowed keys; it also allows
iterating via 'for a in dict.keys():' etc

> wouldn't a = ClText() be better? "class" is a reserved word.

Absolutely, that's what I get for quickly typing up an example.

> It's early morning, but it seems to me that when items() is called, that
is
> when all the entries are actually requested, so that is the latest point
at
> which you can set all the data[key] things from the database values. Use

Sigh :-) This occured to me last night, about ten minutes after sending out
the mail. Can you believe that this kept me busy for more than hours earlier
on? Raah. Anyhow, items(), values() etc are just going to call a setall()
method in case not all keys have been filled with values yet. The database
is in read-only mode during this time.

>   def __getitem__(self, key):
>      return self.data.setdefault(key, DatabaseCall(key))

Need to take a good look at setdefault; didn't even consider this yet.

> The last option is not to use items() :-)

*lol* That was next on my list.

Thanks again to both of you!

 Oliver


--
Oliver Hofmann - University of Cologne - Department of Biochemistry
setar at gmx.de - o.hofmann at smail.uni-koeln.de - connla at thewell.com






More information about the Python-list mailing list