[Grok-dev] Re: Iterativly emptying a grok.container shows weird behavior

Philipp von Weitershausen philipp at weitershausen.de
Tue Jun 12 20:10:07 EDT 2007


Dr. Volker Jaenisch wrote:
> Hello Philipp!
> 
> Philipp von Weitershausen schrieb:
>>
>> To our defence, we nowhere say that containers *are*
> In the tutorial, one read:
>  > From the perspective of Python, you can think of containers as 
> dictionaries. They allow item access (container['key']) to get at its > 
> contents. They also define methods such as keys() and values().

Again, I might be splitting hairs, but "you can think of A as B" isn't 
the same as "A is B". It just says that there's a similarity so striking 
that you don't have to worry about the differences for now.

> I think this formulation is misleading. I am an experianced programmer 
> but the (very good made) tutorial lead me to code this nonsense.
> 
> To be constructive. Why not using the power of definition like:
> 
> "Containers can be accessed like python dictionaries, but internal they 
> are high order datastructures for optimized for performance.
> If you iterate over a container be careful not to change the content 
> while the iteration.
> This example show how not to iterate over a container:
> 
>           for key in self.context.keys() :
>                del self.context[ key ]
> 
> To delete all elements from a container use the containers .clear() method.
> "

That's a good suggestion. The clear() thing is a bit of an edge case, 
though. The more general problem is *inserting* things into a BTree (or 
BTree-based container) while iterating over it or trying to mutate the 
return value of keys()/items() (e.g. getting a sorted keys/items listing).

Either way, a solution for all cases is to list() the return value of 
keys() first (which I thought would be obvious to Python-savvy folks, 
especially since it's the recommended idiom in Py3k).

Anyway, thanks for bringing this to our attention. Martijn seems to want 
to add this to the tutorial, that way it'll hopefully be even less 
misleading in the future.


-- 
http://worldcookery.com -- Professional Zope documentation and training


More information about the Grok-dev mailing list