<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Dave Dash</title>
 <link href="http://davedash.com/tag/reference/atom.xml" rel="self"/>
 <link href="http://davedash.com/tag/reference"/>
 <updated>2012-04-07T22:42:44-07:00</updated>
 <id>http://davedash.com/</id>
 <author>
   <name>Dave Dash</name>
   <email>dd+atom1@davedash.com</email>
 </author>

 
 <entry>
   <title>Python Generators</title>
   <link href="http://davedash.com/2009/04/08/python-generators/"/>
   <updated>2009-04-08T00:00:00-07:00</updated>
   <id>http://davedash.com/2009/04/08/python-generators</id>
   <content type="html">&lt;p&gt;Someone had mentioned &quot;generators&quot; in python to me, so I decided to figure out what it was... and I figured it out.  I think a simple example would help explain it:&lt;/p&gt;

&lt;div&gt;&lt;textarea name=&quot;code&quot; class=&quot;python&quot;&gt;
&gt;&gt;&gt; def fib():
...  i1 = 0
...  i2 = 1
...  while True:
...    yield i2
...    i3 = i2 + i1
...    i1 = i2
...    i2 = i3
... 
&gt;&gt;&gt; a = fib()
&gt;&gt;&gt; a
&lt;generator object at 0x319468&gt;
&gt;&gt;&gt; a.next()
1
&gt;&gt;&gt; a.next()
1
&gt;&gt;&gt; a.next()
2
&gt;&gt;&gt; a.next()
3
&gt;&gt;&gt; a.next()
5
&lt;/textarea&gt;&lt;/div&gt;


&lt;p&gt;Basically you can iterate over this &quot;generator&quot; which is a special function that &quot;yield&quot;s more than one value if you keep pinging it.&lt;/p&gt;

&lt;p&gt;Adding this to the toolkit in my brain.  I'm thinking one potential use is taking a resultset from a datastore (e.g. mysql) and converting it into an object.&lt;/p&gt;
</content>
 </entry>
 

</feed>

