looping list problem

Peter Otten __peter__ at web.de
Tue Aug 16 09:40:53 EDT 2005


Jon Bowlas wrote:

> Ok so I changed it to this:
> 
> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
> for hiddennavelement in hiddennavelements:
>     yield hiddennavelements
> 
> But I get the following error- Line 5: Yield statements are not allowed.

Please show us some more code -- especially the function containing and the
function calling the above chunk.

Generally speaking, a function terminates when execution reaches the first
return statement, e. g.

def f():
    for i in 1, 2, 3:
        return i

will always return 1. A generator, on the other hand,

def g():
    for i in 1, 2, 3:
         yield i

will yield 1, 2, and 3, but the calling code then needs itself a for loop:

for i in g():
    # do something with i

My guess would be that you should either modify your for loop to

> attobject = context.get_attobject()
> navstring = context.get_uclattribute(attobject, 'ucl_navhide')
> hiddennavelements = navstring.split(' ')
for hiddennavelement in hiddennavelements:
   # do something with hiddennavelement

or just return all elements at once

def some_func():
    # ...
    attobject = context.get_attobject()
    navstring = context.get_uclattribute(attobject, 'ucl_navhide')
    return navstring.split(' ')

and then operate on the items in the hiddennavelements list in the calling
code:

for element in some_func():
    # do something with element

Peter













More information about the Python-list mailing list