[Tutor] loops

Lie Ryan lie.1296 at gmail.com
Wed Apr 29 14:25:41 CEST 2009

Norman Khine wrote:
> hello,
> i have the following code:

Uhh... 5 levels deep of nesting, and from the hint that you used 
self.blah earlier this is inside a class which adds a minimum of two 
extra levels, in total of 7 levels nesting... We're in deep trouble.

> is there a way to optimise the loop for 'formats'

In this loop:

for item in result:
     print item.abspath
     item = self.get_handler(item.abspath)
     namespace['item_title'] = item.get_property('dc:title')
     if isinstance(training, Training):
         namespace['item_url'] = item.abspath
         # Need to strip the '/companies' from the path
         #namespace['item_url'] = Path(item.abspath)[1:]
         namespace['item_url'] = Path(item.abspath)

You assigned namespace['item_title'] and namespace['item_url'] several 
times throughout the loop, which means only the last item in result 
would have any lasting significance to the value of 
namespace['item_title'] and namespace['item_url']. You may be able to 
just use result[-1] to directly get the last item.

More information about the Tutor mailing list