Creatively... Do!
  • Home
  • Blog
  • Contact
  • Programming
    • Solitaire
    • Minesweeper
    • Sudoku
    • Pong
    • Mandelbrot
  • Music
    • The Snowman

How to make iterators out of Python functions without using yield

  • Home
  • Blog
  • How to make iterators out of Python functions without using yield
Posted by: christian_abbott 7 years, 5 months ago

(0 comments)

The built-in iter() method in Python 2.2 and later hides a neat trick which is not well-known. Typically iter() is used to return an iterator from an iterable object:

i = iter(iterable_object)
next(i)

However, iter() can also be used in an entirely different way. If you have a function that you need to call multiple times until a particular value is returned, then iter() will let you wrap that function into a convenient iterator. To do this, pass the function as the first argument, and the return value to end with as the second argument:

iter(repeated_function, stop_value)

For example, you could read a text file until you reached a certain text marker:

with open('file.txt') as f:
    for line in iter(f.readline, '#~END_OF_IMPORTANT_TEXT'):
        do_something(line)

One restriction is that the function passed to iter() can't take any arguments. You can use either a lambda expression, or functools.partial() to "fill in" a function's arguments before using it with iter(). For example, here are two ways you could read 4KB at a time from a source object that has a file-like read() method:

# Using a lambda expression
for chunk in iter(lambda: some_source.read(4096), ''):
    do_something(chunk)

# Using functools.partial()
from functools import partial
read_chunk = partial(some_source.read, 4096)
for chunk in iter(read_chunk, ''):
    do_something(chunk)
Current rating: 5
Share on Twitter Share on Facebook
  • ← How to globally customize exception stack traces in Python
  • How to solve the Impossible Escape puzzle with almost no math →

Comments

There are currently no comments

New Comment

required

required (not published)

required

required

Recent Posts

  • Using SVG and CSS to create Pacman (out of pie charts)
  • How to solve the Impossible Escape puzzle with almost no math
  • How to make iterators out of Python functions without using yield
  • How to globally customize exception stack traces in Python
  • Is your tango embrace really too firm or too relaxed?

Archive

2017
  • May (1)
2015
  • February (1)
2013
  • November (1)
  • October (1)
  • March (1)
  • February (3)
  • January (4)
2012
  • December (1)

Categories

  • django (2)
  • environment (1)
  • math (2)
  • python (2)
  • tango (5)
  • web development (1)

Authors

  • christian_abbott (13)

Feeds

RSS / Atom
  • Blog
  • Contact
  • Programming
  • Music
©2012-2020 Christian Abbott