Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a Python generator object?

Consider this scenario:

#!/usr/bin/env python # -*- coding: utf-8 -*- import os  walk = os.walk('/home')  for root, dirs, files in walk:     for pathname in dirs+files:         print os.path.join(root, pathname)  for root, dirs, files in walk:     for pathname in dirs+files:         print os.path.join(root, pathname) 

I know that this example is kinda redundant, but you should consider that we need to use the same walk data more than once. I've a benchmark scenario and the use of same walk data is mandatory to get helpful results.

I've tried walk2 = walk to clone and use in the second iteration, but it didn't work. The question is... How can I copy it? Is it ever possible?

Thank you in advance.

like image 265
Paulo Freitas Avatar asked Feb 09 '11 12:02

Paulo Freitas


People also ask

Can a generator be called multiple times in python?

Yes, generator can be used only once.

How do you create a generator in Python?

It is fairly simple to create a generator in Python. It is as easy as defining a normal function, but with a yield statement instead of a return statement. If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function.

What is a generator expression Python?

A generator expression is an expression that returns a generator object. Basically, a generator function is a function that contains a yield statement and returns a generator object.


2 Answers

You can use itertools.tee():

walk, walk2 = itertools.tee(walk) 

Note that this might "need significant extra storage", as the documentation points out.

like image 126
Sven Marnach Avatar answered Oct 23 '22 01:10

Sven Marnach


If you know you are going to iterate through the whole generator for every usage, you will probably get the best performance by unrolling the generator to a list and using the list multiple times.

walk = list(os.walk('/home'))

like image 33
shang Avatar answered Oct 23 '22 00:10

shang