<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rusty Razor Blade &#187; python</title>
	<atom:link href="http://www.rustyrazorblade.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rustyrazorblade.com</link>
	<description>Tech Thoughts, Mostly on LAMP - by Jon Haddad</description>
	<lastBuildDate>Fri, 03 Feb 2012 20:03:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>New Project: Jester</title>
		<link>http://www.rustyrazorblade.com/2012/02/new-project-jester/</link>
		<comments>http://www.rustyrazorblade.com/2012/02/new-project-jester/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 19:48:16 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[jester]]></category>
		<category><![CDATA[pyparsing]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1580</guid>
		<description><![CDATA[<p>I&#8217;ve started a new open source project called <a href="https://github.com/jhaddad/jester" title="Jester on Github" target="_blank">Jester</a>. Jester is a rules engine for points and badges, also known as <a href="http://en.wikipedia.org/wiki/Gamification" title="Gamification" target="_blank">Gamification</a>.</p> <p>Jester is written in <a href="http://python.org/" title="Python.org" target="_blank">Python</a>, using <a href="http://redis.io/" title="Redis" target="_blank">Redis</a> for storage.</p> <p>I&#8217;ve created a tiny Domain Specific Language for defining rules [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started a new open source project called <a href="https://github.com/jhaddad/jester" title="Jester on Github" target="_blank">Jester</a>.  Jester is a rules engine for points and badges, also known as <a href="http://en.wikipedia.org/wiki/Gamification" title="Gamification" target="_blank">Gamification</a>.</p>
<p>Jester is written in <a href="http://python.org/" title="Python.org" target="_blank">Python</a>, using <a href="http://redis.io/" title="Redis" target="_blank">Redis</a> for storage.</p>
<p>I&#8217;ve created a tiny Domain Specific Language for defining rules using the <a href="http://pyparsing.wikispaces.com/" title="pyparsing" target="_blank">pyparsing</a> library.  </p>
<p>A couple examples of rules:</p>
<blockquote><p>
create rule on game_play award 5 points<br />
create rule on game_play award badge game_addict when game_play occurs 5 times in 1 day
</p></blockquote>
<p>This project is in the very early stages and is not yet functional as of this posting.  However, I expect to have a rough working version of it up by the end of next week.  </p>
<p><a href="https://github.com/jhaddad/jester" title="Jester on Github" target="_blank">Jester on github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2012/02/new-project-jester/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing MySQLdb on MacOS Lion</title>
		<link>http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/</link>
		<comments>http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 06:58:01 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[mac]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1555</guid>
		<description><![CDATA[<p>I ran into an issue installing the <a href="http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/" title="MySQLdb module" target="_blank">MySQLdb module</a>. </p> <p>>>> import MySQLdb<br /> /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.pyc, but /Users/jhaddad/Downloads/MySQL-python-1.2.3 is being added to sys.path<br /> Traceback (most recent call last):<br /> File &#8220;&#8220;, line 1, in <br /> File &#8220;MySQLdb/__init__.py&#8221;, line 19, in <br /> [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an issue installing the <a href="http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/" title="MySQLdb module" target="_blank">MySQLdb module</a>.  </p>
<blockquote><p>>>> import MySQLdb<br />
/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.pyc, but /Users/jhaddad/Downloads/MySQL-python-1.2.3 is being added to sys.path<br />
Traceback (most recent call last):<br />
  File &#8220;<stdin>&#8220;, line 1, in <module><br />
  File &#8220;MySQLdb/__init__.py&#8221;, line 19, in <module><br />
    import _mysql<br />
  File &#8220;build/bdist.macosx-10.7-intel/egg/_mysql.py&#8221;, line 7, in <module><br />
  File &#8220;build/bdist.macosx-10.7-intel/egg/_mysql.py&#8221;, line 6, in __bootstrap__<br />
ImportError: dlopen(/var/root/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib<br />
  Referenced from: /var/root/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so<br />
  Reason: image not found</p></blockquote>
<p>I fixed it by doing the following:</p>
<blockquote><p>sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib</p>
<p>sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql</p></blockquote>
<p>I&#8217;m not sure if it matters, but I&#8217;ve got MySQL 5.5 installed.  </p>
<p>Found the answer here &#8211; but it wasn&#8217;t the top voted one.  If this is useful, vote Chris up.</p>
<p><a href="http://stackoverflow.com/questions/4730787/python-import-mysqldb-error-mac-10-6" target="_blank">http://stackoverflow.com/questions/4730787/python-import-mysqldb-error-mac-10-6</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install issue pymongo on OSX (setuptools out of date)</title>
		<link>http://www.rustyrazorblade.com/2011/04/install-issue-pymongo-on-osx-setuptools-out-of-date/</link>
		<comments>http://www.rustyrazorblade.com/2011/04/install-issue-pymongo-on-osx-setuptools-out-of-date/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 23:33:01 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1416</guid>
		<description><![CDATA[<p>easy_install on the mac has some weird hardcoding issue where it uses a specific version of setuptools. You can read a really good explanation by <a href="http://andreasjacobsen.com/2008/10/10/using-python-setuptools-on-the-mac/">Andreas Jacobsen</a> here.</p> <p>The fix is to use python to call easy_install instead of using the Apple provided one.</p> <p>sudo python -m easy_install pymongo</p>]]></description>
			<content:encoded><![CDATA[<p>easy_install on the mac has some weird hardcoding issue where it uses a specific version of setuptools.  You can read a really good explanation by <a href="http://andreasjacobsen.com/2008/10/10/using-python-setuptools-on-the-mac/">Andreas Jacobsen</a> here.</p>
<p>The fix is to use python to call easy_install instead of using the Apple provided one.</p>
<p><code>sudo python -m easy_install pymongo</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2011/04/install-issue-pymongo-on-osx-setuptools-out-of-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another Attempt At Python</title>
		<link>http://www.rustyrazorblade.com/2011/04/another-attempt-at-python/</link>
		<comments>http://www.rustyrazorblade.com/2011/04/another-attempt-at-python/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 20:32:13 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[erlang]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1390</guid>
		<description><![CDATA[<p>I tried Python out a while ago, but stopped trying it to learn it after some major frustrations. Maybe I didn&#8217;t dig deep enough into it. I found the documentation hard to read, and the module layout seemed a little random at times. For some reason I found <a href="http://docs.python.org/library/subprocess.html">executing an external process</a> and getting [...]]]></description>
			<content:encoded><![CDATA[<p>I tried Python out a while ago, but stopped trying it to learn it after some major frustrations.  Maybe I didn&#8217;t dig deep enough into it.  I found the documentation hard to read, and the module layout seemed a little random at times.  For some reason I found <a href="http://docs.python.org/library/subprocess.html">executing an external process</a> and getting the results to be a little convoluted.  (Since then I&#8217;ve learned to use popen(..).communicate())</p>
<p><a href="http://www.rustyrazorblade.com/wp-content/uploads/2011/04/Cover-Image-For-Seven-Languages-in-Seven-Weeks.jpeg"><img src="http://www.rustyrazorblade.com/wp-content/uploads/2011/04/Cover-Image-For-Seven-Languages-in-Seven-Weeks.jpeg" alt="" title="Cover Image For Seven Languages in Seven Weeks" width="190" height="228" class="alignright size-full wp-image-1411" /></a>I ended up messing with other languages to try to find one that suits my tastes, like <a href="http://www.erlang.org/">Erlang</a> and <a href="http://www.digitalmars.com/d/index.html">D</a>.  I read through <a href="http://pragprog.com/titles/btlang/seven-languages-in-seven-weeks">7 languages in 7 weeks</a>, and not really getting a lot out of it.  I didn&#8217;t fall in love with <a href="http://www.ruby-lang.org/en/">ruby</a> at all and I&#8217;m not going to actually use <a href="http://www.swi-prolog.org/">Prolog</a> anywhere, even if I thought it was pretty cool.  I never liked <a href="http://www.java.com/en/">Java</a>, and I wasn&#8217;t impressed with <a href="http://www.scala-lang.org/">Scala</a>. </p>
<p>The closest I&#8217;ve gotten to a new favorite is Erlang.  I&#8217;ve written a <a href="/category/erlang/">whole slew of posts</a> about it.  I like Erlang&#8217;s process management, message passing, and the OTP libraries.  I like talking about it with people and I advocate it&#8217;s advantages.  But when it comes time to write it, I find it to be clumsy and awkward, the syntax annoying and the external libraries painful.  For instance, trying to work with a MongoDB database just doesn&#8217;t feel right.  My mind likes OO code, and I&#8217;m done fighting it.  So time to try something else.</p>
<p>I thought D was really interesting, but unfortunately it looks like no one is using it.  It&#8217;s not even clear which compiler I should use, and the installation instructions were incorrect when I tried to set it up.  Not a big selling point, and I gave that up pretty quickly.</p>
<p><a href="http://www.rustyrazorblade.com/wp-content/uploads/2011/04/python-logo.gif"><img src="http://www.rustyrazorblade.com/wp-content/uploads/2011/04/python-logo.gif" alt="" title="python-logo" width="211" height="71" class="alignright size-full wp-image-1408" /></a>So, another look at Python.  Working with threads in Python kind of sucks.  If you have multiple cores, it doesn&#8217;t really care.  That&#8217;s unfortunate.  However, after reading through convos in IRC (#python on freenode) I&#8217;ve found a few things that are making me give it another look.  For instance, the <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing module</a> seems to address my gripes about processes / threads.  Yes we&#8217;re creating OS processes, but I really only need a few (5, 10 maybe?) so having a few running shouldn&#8217;t be a problem, and the OS will take care of loading up my cores for me.  It&#8217;s got great support for MongoDB (my shiny new toy) and I like it&#8217;s clean syntax.  I think tornado looks really cool and I&#8217;m excited to write a stand alone server that I can bundle instead of requiring nginx configs and complicated setups.</p>
<p>So&#8230; I&#8217;m back writing Python.  We&#8217;ll see how it goes.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2011/04/another-attempt-at-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python Module Docs</title>
		<link>http://www.rustyrazorblade.com/2010/04/python-module-docs/</link>
		<comments>http://www.rustyrazorblade.com/2010/04/python-module-docs/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 00:12:05 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=1076</guid>
		<description><![CDATA[<p>I&#8217;ve been trying to get into Python in my spare time, since it&#8217;s got such a huge volume of modules and looks like it should be easy to be ripping it up in no time. But of course, I have my complaints.</p> <p>Fortunately I don&#8217;t need to write a long blog post, <a href="http://www.russellbeattie.com/blog/python-library-docs-still-suck">this guy [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been trying to get into Python in my spare time, since it&#8217;s got such a huge volume of modules and looks like it should be easy to be ripping it up in no time.    But of course, I have my complaints.</p>
<p>Fortunately I don&#8217;t need to write a long blog post, <a href="http://www.russellbeattie.com/blog/python-library-docs-still-suck">this guy did it for me.</a>  It&#8217;s kind of alarming this post is from 2 years ago and the docs are still a major problem.</p>
<p>I&#8217;ve got a gripe with every language (who doesn&#8217;t?) but this is really killing me.  Such great potential.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2010/04/python-module-docs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyDev Tutorial</title>
		<link>http://www.rustyrazorblade.com/2009/11/pydev-tutorial/</link>
		<comments>http://www.rustyrazorblade.com/2009/11/pydev-tutorial/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 20:54:23 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=899</guid>
		<description><![CDATA[<p>I found a good <a href="http://www.ibm.com/developerworks/library/os-ecant/">tutorial</a> on IBM on doing Python development with Eclipse. It might be a little out of date, but I think only the screens got moved around a little bit. It includes details on how to use ant, which I&#8217;ve recently started using with cruise control and PHP, so I&#8217;m becoming [...]]]></description>
			<content:encoded><![CDATA[<p>I found a good <a href="http://www.ibm.com/developerworks/library/os-ecant/">tutorial</a> on IBM on doing Python development with Eclipse.  It might be a little out of date, but I think only the screens got moved around a little bit.  It includes details on how to use ant, which I&#8217;ve recently started using with cruise control and PHP, so I&#8217;m becoming a fan.</p>
<p>I did run into an issue where I&#8217;d get the error &#8216;Variable references empty selection: ${project_loc}”, but a quick google brought me to a solution <a href="http://juddsolutions.blogspot.com/2008/07/eclipse-projectloc-errors-during-grails.html">here</a>.</p>
<p>I only had PDT installed, which doesn&#8217;t come with the Ant tools.  You just need to install the Eclipse Java Development Tools.  Found that tip on <a href="http://stackoverflow.com/questions/417074/eclipse-pdt-ant">Stack Overflow</a>.</p>
<p>Otherwise, a great tutorial on how to get working with Eclipse and Python.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/11/pydev-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PyTables user&#8217;s guide</title>
		<link>http://www.rustyrazorblade.com/2009/10/pytables-users-guide/</link>
		<comments>http://www.rustyrazorblade.com/2009/10/pytables-users-guide/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 03:01:06 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[pytables]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/2009/10/pytables-users-guide/</guid>
		<description><![CDATA[<p>I finally found the manual for PyTables. In case anyone else is as blind as I am, I&#8217;m linking to it <a href="http://www.pytables.org/docs/manual/index.html">here</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>I finally found the manual for PyTables.  In case anyone else is as blind as I am, I&#8217;m linking to it <a href="http://www.pytables.org/docs/manual/index.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/10/pytables-users-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing NumPy on MacOS X Snow Leopard</title>
		<link>http://www.rustyrazorblade.com/2009/09/installing-numpy/</link>
		<comments>http://www.rustyrazorblade.com/2009/09/installing-numpy/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 19:24:08 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[fortran]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[numpy]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=853</guid>
		<description><![CDATA[<p><a href="http://numpy.scipy.org/">NumPy</a> is a requirement to work with <a href="http://www.pytables.org/moin">PyTables</a>. This is the second step in the install process, after getting <a href="/2009/09/issues-compiling-hdf5-on-macos-x-snow-leopard/">HDF5 set up.</a></p> <p>These instructions are based on the ones found on the NumPy site, but I&#8217;m summarizing things for my own use later on.</p> <p>First, you&#8217;ll need to install the Fortran compiler [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://numpy.scipy.org/">NumPy</a> is a requirement to work with <a href="http://www.pytables.org/moin">PyTables</a>.  This is the second step in the install process, after getting <a href="/2009/09/issues-compiling-hdf5-on-macos-x-snow-leopard/">HDF5 set up.</a></p>
<p>These instructions are based on the ones found on the NumPy site, but I&#8217;m summarizing things for my own use later on.</p>
<p>First, you&#8217;ll need to install the Fortran compiler for OSX (gFortran).  Fortunately there&#8217;s a <a href="http://r.research.att.com/gfortran-4.2.3.dmg">Fortran universal binary installer.</a> </p>
<p>Next, get the NumPy source.  You can <a href="http://sourceforge.net/projects/numpy/files/">find it on Sourceforge</a>.  </p>
<p>Now, build with gFortran.</p>
<blockquote><p>python setup.py build &#8211;fcompiler=gnu95<br />
python setup.py install
</p></blockquote>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/09/installing-numpy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Issues Compiling HDf5 1.8.3 on MacOS X Snow Leopard</title>
		<link>http://www.rustyrazorblade.com/2009/09/issues-compiling-hdf5-on-macos-x-snow-leopard/</link>
		<comments>http://www.rustyrazorblade.com/2009/09/issues-compiling-hdf5-on-macos-x-snow-leopard/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 19:01:03 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[hdf5]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=848</guid>
		<description><![CDATA[<p>I&#8217;m trying to evaluate <a href="http://www.pytables.org/moin">pytables</a> as a replacement for very large Python dictionaries, but having some issues getting HDF5 installed on my Mac (OS X Snow Leopard). </p> <p>I&#8217;ve been getting this error:</p> <p>configure: error: C compiler cannot create executables</p> <p>I haven&#8217;t been able to figure out what&#8217;s wrong yet &#8211; anyone have any [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying to evaluate <a href="http://www.pytables.org/moin">pytables</a> as a replacement for very large Python dictionaries, but having some issues getting HDF5 installed on my Mac (OS X Snow Leopard).  </p>
<p>I&#8217;ve been getting this error:</p>
<blockquote><p>configure: error: C compiler cannot create executables</p></blockquote>
<p>I haven&#8217;t been able to figure out what&#8217;s wrong yet &#8211; anyone have any ideas?  I&#8217;ve got XCode Tools installed, I&#8217;ve compiled Apache, PHP and Memcached without issue (prior to Snow Leopard Update).</p>
<p>Help, anyone?</p>
<p><em>Edit</em>: I just found a <a href="http://discussions.apple.com/thread.jspa?threadID=2132536&#038;tstart=0">post in the Apple discussion boards</a> about this being a Snow Leopard specific issue.  The suggestion is to install XCode off the Snow Leopard disc.  I&#8217;ve reinstalled XCode, and now it successfully runs the configure command.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2009/09/issues-compiling-hdf5-on-macos-x-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mutable class instance variables in Python act like they&#8217;re static</title>
		<link>http://www.rustyrazorblade.com/2008/05/class-instance-variables-in-python-act-like-theyre-static/</link>
		<comments>http://www.rustyrazorblade.com/2008/05/class-instance-variables-in-python-act-like-theyre-static/#comments</comments>
		<pubDate>Thu, 29 May 2008 07:45:39 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.rustyrazorblade.com/?p=132</guid>
		<description><![CDATA[<p>There&#8217;s a weird behavior in Python when dealing with Mutable types such as dictionaries, that when you modify a variable defined as a class attribute, you&#8217;re actually modifying a shared dictionary amongst all the classes. This seemed weird to me. You can <a href="http://mail.python.org/pipermail/python-list/2001-June/088098.html">read the lovely discussion</a> about it, if you want. Or, just follow [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a weird behavior in Python when dealing with Mutable types such as dictionaries, that when you modify a variable defined as a class attribute, you&#8217;re actually modifying a shared dictionary amongst all the classes.  This seemed weird to me.  You can <a href="http://mail.python.org/pipermail/python-list/2001-June/088098.html">read the lovely discussion</a> about it, if you want.  Or, just follow my code for a demo on how to deal with the issue.  I just started Python on Monday night, so please overlook my n00bness.</p>
<blockquote><pre>
## the first test will use an int, which is immutable.
class test1:
	i = 0
	def add(self):
		self.i = self.i + 1

t = test1()
t.add()
t2 = test1()
t2.add()
t3 = test1()
t3.add()

## this will output 1, cool
print t3.i

## Lets try it again with a dictionary - which is a mutable type
class test2:
	i = {} ## lets use a dictionary
	def add(self, str):
		self.i[str] = 1;

t = test2()
t.add('hi');
t.add('steve');
print t.i
# Will print the expected:
# {'steve': 1, 'hi': 1}

t2 = test2()
t2.add('bacon')
print t2.i

# But this prints:
# {'steve': 1, 'hi': 1, 'bacon': 1}
# Unexpected, in most languages

## it's necessary to define test2 in the following manner to avoid this weirdness

class test2:
	def __init__(self):
		self.i = {}
	def add(self, str):
		self.i[str] = 1;

t = test2()
t.add('hi');
t.add('steve');
print t.i
# Will print the expected:
# {'steve': 1, 'hi': 1}

t2 = test2()
t2.add('bacon')
print t2.i		

# Will now print the expected:
# {'bacon': 1}
</pre>
</blockquote>
<p>Reading the link above is a good idea if you&#8217;re interested to know why something works the way it does.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.rustyrazorblade.com/2008/05/class-instance-variables-in-python-act-like-theyre-static/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

