<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: There are more than one way to do it</title>
	<atom:link href="http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/</link>
	<description>a brain backup</description>
	<pubDate>Thu, 29 Jul 2010 14:16:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: tripu</title>
		<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/comment-page-1/#comment-250</link>
		<dc:creator>tripu</dc:creator>
		<pubDate>Thu, 23 Apr 2009 12:34:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.lorenzogil.com/blog/?p=110#comment-250</guid>
		<description>The shortest one, no matter what!</description>
		<content:encoded><![CDATA[<p>The shortest one, no matter what!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lgs</title>
		<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/comment-page-1/#comment-249</link>
		<dc:creator>lgs</dc:creator>
		<pubDate>Tue, 21 Apr 2009 18:56:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.lorenzogil.com/blog/?p=110#comment-249</guid>
		<description>Thanks for your replies, Andrew and Adomas!!

I love your solutions and they truly feel pythonic. Thanks for teaching me. However the method I post here was a simplified version of the real method. In the real one, I pass some arguments to the collector(destination) function that depends on the number of iterations.

Thanks again!</description>
		<content:encoded><![CDATA[<p>Thanks for your replies, Andrew and Adomas!!</p>
<p>I love your solutions and they truly feel pythonic. Thanks for teaching me. However the method I post here was a simplified version of the real method. In the real one, I pass some arguments to the collector(destination) function that depends on the number of iterations.</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adomas Paltanavicius</title>
		<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/comment-page-1/#comment-247</link>
		<dc:creator>Adomas Paltanavicius</dc:creator>
		<pubDate>Sun, 19 Apr 2009 22:40:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.lorenzogil.com/blog/?p=110#comment-247</guid>
		<description>1 from itertools import chain, islice
2 
3 for photo in islice(chain(*self.collectors), self.max):
4     self.add_photo(photo)
5
6 return self.get_photos()</description>
		<content:encoded><![CDATA[<p>1 from itertools import chain, islice<br />
2<br />
3 for photo in islice(chain(*self.collectors), self.max):<br />
4     self.add_photo(photo)<br />
5<br />
6 return self.get_photos()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Durdin</title>
		<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/comment-page-1/#comment-245</link>
		<dc:creator>Andrew Durdin</dc:creator>
		<pubDate>Sun, 19 Apr 2009 21:32:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.lorenzogil.com/blog/?p=110#comment-245</guid>
		<description>As I was afraid, the indentation of that was lost.  Here it is again with leading non-breaking spaces. I hope they work better:

1  def collect_photos(self, destination):
2      from itertools import islice

3      def photos_fetcher():
4          for collector in self.collectors:
5              for photo in collector(destination):
6                  yield photo

7      for photo_info in islice(photos_fetcher(), self.max):
8          self.add_photo(photo_info)

9      return self.get_photos()</description>
		<content:encoded><![CDATA[<p>As I was afraid, the indentation of that was lost.  Here it is again with leading non-breaking spaces. I hope they work better:</p>
<p>1  def collect_photos(self, destination):<br />
2      from itertools import islice</p>
<p>3      def photos_fetcher():<br />
4          for collector in self.collectors:<br />
5              for photo in collector(destination):<br />
6                  yield photo</p>
<p>7      for photo_info in islice(photos_fetcher(), self.max):<br />
8          self.add_photo(photo_info)</p>
<p>9      return self.get_photos()</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Durdin</title>
		<link>http://www.lorenzogil.com/blog/2009/04/19/there-are-more-than-one-way-to-do-it/comment-page-1/#comment-244</link>
		<dc:creator>Andrew Durdin</dc:creator>
		<pubDate>Sun, 19 Apr 2009 21:31:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.lorenzogil.com/blog/?p=110#comment-244</guid>
		<description>Of these, none of them seem to me to quite as simple as they might be.  The itertools module has quite a lot of tools to help with simple tasks like this.
Here I build a generator that yields photos sequentially for as long as possible; then use islice() to limit it to no more than self.max.  If there are fewer photos than this, then that for loop will just end a little sooner:


1  def collect_photos(self, destination):
2      from itertools import islice

3      def photos_fetcher():
4          for collector in self.collectors:
5              for photo in collector(destination):
6                  yield photo

7      for photo_info in islice(photos_fetcher(), self.max):
8          self.add_photo(photo_info)

9      return self.get_photos()


As a side note, you should rarely (in Python) need to manually increment a variable to count elements in an iterable; instead, use enumerate(), or itertools.izip() together with itertools.count() if you need more than 0-indexed incrementing.


"Then I tried to convert the for loops into while loops since basic computer science tell us that a while loop is the choice to make when the number of iterations is unknown."

In Python, I'd actually argue in favour of using iterators/generators and for loops in preference to while loops, as the termination conditions are usually a lot clearer, and the resulting code is often more succint.</description>
		<content:encoded><![CDATA[<p>Of these, none of them seem to me to quite as simple as they might be.  The itertools module has quite a lot of tools to help with simple tasks like this.<br />
Here I build a generator that yields photos sequentially for as long as possible; then use islice() to limit it to no more than self.max.  If there are fewer photos than this, then that for loop will just end a little sooner:</p>
<p>1  def collect_photos(self, destination):<br />
2      from itertools import islice</p>
<p>3      def photos_fetcher():<br />
4          for collector in self.collectors:<br />
5              for photo in collector(destination):<br />
6                  yield photo</p>
<p>7      for photo_info in islice(photos_fetcher(), self.max):<br />
8          self.add_photo(photo_info)</p>
<p>9      return self.get_photos()</p>
<p>As a side note, you should rarely (in Python) need to manually increment a variable to count elements in an iterable; instead, use enumerate(), or itertools.izip() together with itertools.count() if you need more than 0-indexed incrementing.</p>
<p>&#8220;Then I tried to convert the for loops into while loops since basic computer science tell us that a while loop is the choice to make when the number of iterations is unknown.&#8221;</p>
<p>In Python, I&#8217;d actually argue in favour of using iterators/generators and for loops in preference to while loops, as the termination conditions are usually a lot clearer, and the resulting code is often more succint.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
