title: How to len(generator())
body: <p><a href="http://docs.python.org/tutorial/classes.html#generators" rel="noreferrer">Python generators</a> are very useful. They have advantages over functions that return lists. However, you could <code>len(list_returning_function())</code>. Is there a way to <code>len(generator_function())</code>?</p>
  
  <p><strong>UPDATE:</strong><br>
  Of course <code>len(list(generator_function()))</code> would work.....<br>
  I'm trying to use a generator I've created inside a new generator I'm creating. As part of the calculation in the new generator it needs to know the length of the old one. However I would like to keep both of them together with the same properties as a generator, specifically - not maintain the entire list in memory as it may be <strong>very</strong> long.</p>
  
  <p><strong>UPDATE 2:</strong><br>
  Assume the generator <em>knows</em> it's target length even from the first step. Also, there's no reason to maintain the <code>len()</code> syntax. Example - if functions in Python are objects, couldn't I assign the length to a variable of this object that would be accessible to the new generator?</p>