RE: looping list problem

Discussion in 'Python' started by Jon Bowlas, Aug 16, 2005.

  1. Jon Bowlas

    Jon Bowlas Guest

    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.

    Any ideas

    -----Original Message-----
    From: python-list-bounces+me=
    [mailto:python-list-bounces+me=] On Behalf Of
    Fredrik Lundh
    Sent: 16 August 2005 13:44
    To:
    Subject: Re: looping list problem

    Jon Bowlas wrote:

    > attobject = context.get_attobject()
    > navstring = context.get_uclattribute(attobject, 'ucl_navhide')
    > hiddennavelements = navstring.split(' ')
    > for hiddennavelement in hiddennavelements:
    > return hiddennavelement
    >
    > So the script 'get_attobject' basically looks for an instance of the
    > attributes object in the current folder, if it doesn't locate one then it
    > uses acquisition to find one in a parent folder. The script
    > 'get_uclattribute' then gets the nodeValues of the requested node. In this
    > instance its ucl_navhide, then I split the 'navstring' string at the

    spaces
    > and attempt the for-loop to output each of the values.
    >
    > Unfortunately it appears I am unable to loop through each of the list

    items
    > in hiddennavelements, as it only returns the first value & will not

    repeat.

    did you perhaps mean to use "yield" instead of "return" ?

    </F>



    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Jon Bowlas, Aug 16, 2005
    #1
    1. Advertising

  2. Jon Bowlas

    Peter Hansen Guest

    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 post the full traceback (cut and paste). Don't just retype the
    error message like that.

    -Peter
     
    Peter Hansen, Aug 16, 2005
    #2
    1. Advertising

  3. Jon Bowlas

    Peter Otten Guest

    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
     
    Peter Otten, Aug 16, 2005
    #3
  4. On Tue, 16 Aug 2005 14:24:55 +0100, "Jon Bowlas" <>
    declaimed the following in comp.lang.python:


    > for hiddennavelement in hiddennavelements:
    > yield hiddennavelements
    >

    Well, off the top of my head... Shouldn't that yield refer to the
    singular loop variable and not the plural list of items?
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Aug 17, 2005
    #4
  5. On Tue, 16 Aug 2005 16:46:06 +0100, "Jon Bowlas" <>
    declaimed the following in comp.lang.python:


    Your indentation (at least as posted) is a shambles...
    > for c in node.get_ordered_publishables():
    > if not c.is_published():
    > continue
    > bert=c.getId();
    > if bert=="index_right":
    > continue
    > if bert=="images":
    > continue
    > # this is where I loop through he elements returned by my hiddens function,
    > comparing them with the value of bert
    > for element in hiddens():


    Going by the indentation here, the for loop is part of the "if
    bert=='images':" block... But since the first statement in that block is
    "continue", the for loop will never execute.

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Aug 17, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. saoirse_79

    looping through a list of lists.

    saoirse_79, Oct 8, 2003, in forum: Python
    Replies:
    0
    Views:
    250
    saoirse_79
    Oct 8, 2003
  2. Rob Hunter

    Re: looping through a list of lists.

    Rob Hunter, Oct 8, 2003, in forum: Python
    Replies:
    2
    Views:
    305
    anton muhin
    Oct 8, 2003
  3. Jon Bowlas

    looping list problem

    Jon Bowlas, Aug 16, 2005, in forum: Python
    Replies:
    1
    Views:
    274
    Paul McGuire
    Aug 16, 2005
  4. Andy
    Replies:
    3
    Views:
    487
    James Kanze
    Jun 8, 2007
  5. Replies:
    5
    Views:
    279
Loading...

Share This Page