Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:03:43+00:00 2026-06-10T14:03:43+00:00

I am missing something about how recursion work in Python. I have put in

  • 0

I am missing something about how recursion work in Python. I have put in place the following method in place to tokenize a sentence:

def extractIngredientInfo(ingredientLine, sectionTitle):

    print 'extractIngredientInfo' + ingredientLine

    # set-up some default values for variables that will contains the extracted datas
    usmeas             = 'N.A'
    othermeas          = 'N.A'

    p_ingredientalt     = re.compile('\(or\s(.*?)\)')
    malt = p_ingredientalt.search(ingredientLine)
    if malt:
        ingredientAlt = malt.group(1)
        ingredientLine = ingredientLine.replace(malt.group(0), '').strip()
        print 'NEW LINE TO TREAT(ALT)' + ingredientLine
        extractIngredientInfo(ingredientLine, sectionTitle)
        usmeas,othermeas = extractOneIngredientInfo(ingredientAlt)
        print 'MALT'
        ingredient 
        yield usmeas, othermeas
        #return;

    p_ingredientpurpose = re.compile('\(for\s(.*?)\)') 
    mpurpose = p_ingredientpurpose.search(ingredientLine)
    if mpurpose:
        ingredientPurpose = mpurpose.group(1)
        ingredientLine = ingredientLine.replace(mpurpose.group(0), '').strip()
        print 'NEW LINE TO TREAT(FOR)' + ingredientLine
        extractIngredientInfo(ingredientLine, sectionTitle)
        usmeas,othermeas = extractOneIngredientInfo(ingredientPurpose)
        print 'MPURPOSE'
        yield usmeas,othermeas
        #return;

    usmeas,othermeas = extractOneIngredientInfo(ingredientLine)
    print 'FINAL'
    yield usmeas, othermeas

when i am making a calling to this function, I have a match for malt which should lead to an immediate call to the recursive function extractIngredientInfo but this never happening (I don’t see the second call to print 'extractIngredientInfo' + ingredientLine. Is there any specific reason this is not happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:03:45+00:00Added an answer on June 10, 2026 at 2:03 pm

    Your function returns a generator because it uses yield statements. A generator is paused until you request the next value.

    This means that a generator function doesn’t do anything until you call .next() it, or use it as an iterator in a loop:

    >>> def foo():
    ...     print 'Foo called'
    ...     yield 'bar'
    ...     print 'Still in foo'
    ...     yield 'baz'
    ... 
    >>> foogen = foo()
    >>> foogen.next()
    Foo called
    'bar'
    >>> foogen.next()
    Still in foo
    'baz'
    >>> for val in foo():
    ...     pass
    ... 
    Foo called
    Still in foo
    

    Notice how the message Foo called is not printed until I call .next() on the generator.

    You only call your recursive function, but that returns a generator that you then discard. The code itself is never executed because it is kept on hold. Loop over the results instead:

    for res in extractIngredientInfo(ingredientLine, sectionTitle):
        yield res
    

    Now you actually iterate over the nested generator function results, and pass them on to the caller (the consumer of the outer nested generator function).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I must be missing something obvious about how the various Scanner methods work but
Maybe I'm missing something about the way Gradle works. What I have here is
OK I have looked and looked and I think I am missing something about
I may be missing something about the fundamentals of WPF design, but I was
I think I am missing something about correct behaviour of Monitor.Enter and Monitor.TryEnter .
I think I am missing something about the sphinx extension for doctest. The typical
I think I am missing something about property attributes. First, I can't understand the
Perhaps I'm missing something about using gi through svn, but how do I keep
Maybe I'm totally missing something about even handling in jQuery, but here's my problem.
I must be missing something about how people do this in Ruby. If '#protected'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.