<p>What's the best Python idiom for this C construct?</p>

<pre><code>while ((x = next()) != END) {
    ....
}
</code></pre>

<p>I don't have the ability to recode next().</p>

<p>update:  and the answer from seems to be:</p>

<pre><code>for x in iter(next, END):
    ....
</code></pre>

[{'answer_id': 28563, 'author': 'Blair Conrad', 'author_id': 1199, 'author_profile': 'https://Stackoverflow.com/users/1199', 'pm_score': 2, 'selected': False, 'text': "<p>Maybe it's not terribly idiomatic, but I'd be inclined to go with</p>\n\n<pre><code>x = next()\nwhile x != END:\n    do_something_with_x\n    x = next()\n</code></pre>\n\n<p>... but that's because I find that sort of thing easy to read</p>\n"}, {'answer_id': 28566, 'author': 'FreeMemory', 'author_id': 2132, 'author_profile': 'https://Stackoverflow.com/users/2132', 'pm_score': 1, 'selected': False, 'text': "<p>What are you trying to do here?\nIf you're iterating over a list, you can use <code>for e in L</code> where e is the element and L is the list. If you're filtering a list, you can use list comprehensions (i.e. <code>[ e for e in L if e % 2 == 0 ]</code> to get all the even numbers in a list).</p>\n"}, {'answer_id': 28568, 'author': 'Johannes Hoff', 'author_id': 3102, 'author_profile': 'https://Stackoverflow.com/users/3102', 'pm_score': 3, 'selected': False, 'text': '<p>It depends a bit what you want to do. To match your example as far as possible, I would make next a generator and iterate over it:</p>\n\n<pre><code>def next():\n   for num in range(10):\n      yield num\n\nfor x in next():\n   print x\n</code></pre>\n'}, {'answer_id': 28580, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 1, 'selected': False, 'text': '<p>If you need to do this more than once, the pythonic way would use an iterator</p>\n\n<pre><code>for x in iternext():\n    do_something_with_x\n</code></pre>\n\n<p>where <code>iternext</code> would be defined using something like\n(<a href="http://www.python.org/dev/peps/pep-0020/" rel="nofollow noreferrer">explicit is better than implicit!</a>):</p>\n\n<pre><code>def iternext():\n    x = next()\n    while x != END:\n        yield x\n        x = next()        \n</code></pre>\n'}, {'answer_id': 28714, 'author': 'FreeMemory', 'author_id': 2132, 'author_profile': 'https://Stackoverflow.com/users/2132', 'pm_score': 3, 'selected': True, 'text': '<p>Short answer: there\'s no way to do inline variable assignment in a while loop in Python. Meaning that I <strong>cannot</strong> say:</p>\n\n<pre><code>while x=next():\n    // do something here!\n</code></pre>\n\n<p>Since that\'s not possible, there are a number of "idiomatically correct" ways of doing this:</p>\n\n<pre><code>while 1:\n    x = next()\n    if x != END:\n        // Blah\n    else:\n        break\n</code></pre>\n\n<p>Obviously, this is kind of ugly. You can also use one of the "iterator" approaches listed above, but, again, that may not be ideal. Finally, you can use the "pita pocket" approach that I actually just found while googling:</p>\n\n<pre><code>class Pita( object ):\n    __slots__ = (\'pocket\',)\n    marker = object()\n    def __init__(self, v=marker):\n        if v is not self.marker:\n            self.pocket = v\n    def __call__(self, v=marker):\n        if v is not self.marker:\n            self.pocket = v\n        return self.pocket\n</code></pre>\n\n<p>Now you can do:</p>\n\n<pre><code>p = Pita()\nwhile p( next() ) != END:\n    // do stuff with p.pocket!\n</code></pre>\n\n<p>Thanks for this question; learning about the <code>__call__</code> idiom was really cool! :)</p>\n\n<p>EDIT: I\'d like to give credit where credit is due. The \'pita pocket\' idiom was found <a href="http://mail.python.org/pipermail/python-list/2003-July/216789.html" rel="nofollow noreferrer">here</a></p>\n'}, {'answer_id': 28780, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 1, 'selected': False, 'text': "<p>Can you provide more information about what you're trying to accomplish?  It's not clear to me why you can't just say</p>\n\n<pre><code>for x in everything():\n    ...\n</code></pre>\n\n<p>and have the everything function return everything, instead of writing a next function to just return one thing at a time.  Generators can even do this quite efficiently.</p>\n"}, {'answer_id': 426415, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 4, 'selected': False, 'text': '<p>@Mark Harrison\'s answer:</p>\n\n<pre><code>for x in iter(next_, END):\n    ....\n</code></pre>\n\n<p>Here\'s an excerpt from <a href="http://docs.python.org/library/functions.html" rel="noreferrer">Python\'s documentation</a>:</p>\n\n<pre><code>iter(o[, sentinel])\n</code></pre>\n\n<blockquote>\n  <p>Return an iterator object.\n  <em>...(snip)...</em> If the second argument, <code>sentinel</code>, is given, then <code>o</code> must be\n  a callable object. The iterator\n  created in this case will call <code>o</code>\n  with no arguments for each call to its\n  <code>next()</code> method; if the value returned\n  is equal to <code>sentinel</code>,\n  <code>StopIteration</code> will be raised,\n  otherwise the value will be returned.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/28559', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']