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

  • Home
  • SEARCH
  • 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 693693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:46:48+00:00 2026-05-14T02:46:48+00:00

I have studied generators feature and i think i got it but i would

  • 0

I have studied generators feature and i think i got it but i would like to understand where i could apply it in my code.

I have in mind the following example i read in “Python essential reference” book:

# tail -f
 def tail(f):
  f.seek(0,2) 
  while True:
   line = f.readline() 
   if not line: 
     time.sleep(0.1)
     continue
   yield line

Do you have any other effective example where generators are the best tool for the job like tail -f?

How often do you use generators feature and in which kind of functionality\part of program do you usually apply it?

  • 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-05-14T02:46:48+00:00Added an answer on May 14, 2026 at 2:46 am

    I use them a lot when I implement scanners (tokenizers) or when I iterate over data containers.

    Edit: here is a demo tokenizer I used for a C++ syntax highlight program:

    whitespace = ' \t\r\n'
    operators = '~!%^&*()-+=[]{};:\'"/?.,<>\\|'
    
    def scan(s):
        "returns a token and a state/token id"
        words = {0:'', 1:'', 2:''} # normal, operator, whitespace
        state = 2 # I pick ws as first state
        for c in s:
            if c in operators:
                if state != 1:
                    yield (words[state], state)
                    words[state] = ''
                state = 1
                words[state] += c
            elif c in whitespace:
                if state != 2:
                    yield (words[state], state)
                    words[state] = ''
                state = 2
                words[state] += c
            else:
                if state != 0:
                    yield (words[state], state)
                    words[state] = ''
                state = 0
                words[state] += c
        yield (words[state], state)
    

    Usage example:

    >>> it = scan('foo(); i++')
    >>> it.next()
    ('', 2)
    >>> it.next()
    ('foo', 0)
    >>> it.next()
    ('();', 1)
    >>> it.next()
    (' ', 2)
    >>> it.next()
    ('i', 0)
    >>> it.next()
    ('++', 1)
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this issue working but i would like to know if there is
I have studied preference activity but I don't understand it. Can anybody tell me
i have studied design patterns and want to use them to code an open
I think I have studied all the duplicates. I have built Mercurial 2.0.2 and
I have some fundamental points/questions about OpenGL, not all involving code but concepts as
I have studied that during a fork, the data and code segment of the
I have studied several articles, some chapters of books about FP and I would
I have just studied the RegEx and I find myself confusing from this following
I'm the newcomer into java ee,I have studied about core java,servlet,jsp. Could anyone give
I have studied the use of Namespaces in PHP a while back but recently

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.