<?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>Wazi &#187; Devon Jones</title>
	<atom:link href="http://olex.openlogic.com/wazi/author/devonjones/feed/" rel="self" type="application/rss+xml" />
	<link>http://olex.openlogic.com/wazi</link>
	<description>Open Source Articles, Tutorials and Licensing Information</description>
	<lastBuildDate>Mon, 30 Aug 2010 20:40:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Python Takes A Great Step Forward: Python 3</title>
		<link>http://olex.openlogic.com/wazi/2008/python-takes-a-great-step-forward-python-3/</link>
		<comments>http://olex.openlogic.com/wazi/2008/python-takes-a-great-step-forward-python-3/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 21:22:47 +0000</pubDate>
		<dc:creator>Devon Jones</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://olex.openlogic.com/wazi/?p=1576</guid>
		<description><![CDATA[Last week saw the release of a major revision to the Python language in the form of Python 3.  The 3.0 release comes with a slew of updates to the standard library, changes in syntax and other requested improvements.  If one thing can be said about the 3.0 release, it's that...]]></description>
			<content:encoded><![CDATA[<div style='padding:10px;border:1px solid;margin-bottom:20px;'>This article, <a href='http://olex.openlogic.com/wazi/2008/python-takes-a-great-step-forward-python-3/'>Python Takes A Great Step Forward: Python 3</a>, originally appeared on <a
      href='http://olex.openlogic.com/wazi'>Wazi</a>, a free source for news, information, and articles on open source software.</div><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Folex.openlogic.com%2Fwazi%2F2008%2Fpython-takes-a-great-step-forward-python-3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Folex.openlogic.com%2Fwazi%2F2008%2Fpython-takes-a-great-step-forward-python-3%2F&amp;source=OpenLogic&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Last week saw the release of a major revision to the <a href="https://olex.openlogic.com/packages/python">Python</a> language in the form of Python 3.  The 3.0 release comes with a slew of updates to the standard library, changes in syntax and other requested improvements.  If one thing can be said about the 3.0 release, it&#8217;s that it is a major step forward for Python and it&#8217;s guiding philosophy of &#8220;There should be one &#8211; and preferably only one &#8211; obvious way to do it&#8221;.</p>
<h3>What&#8217;s Different</h3>
<p>Much of the cruft that had accumulated over the 2.x releases has been removed, and the language has received a serious amount of well thought out tidying.  Unfortunately, nothing in this world is free, and the exceptional cleanness of Python 3 comes at the cost of backwards compatibility.  Still, the cost appears to be worth the price as the update is less error prone, more internally consistent and easier to maintain.  This greater consistency results in a language that is easier to learn. With whole classes of defects effectively eliminated, Python 3 is a safer bet as well.</p>
<p>A selection of the bigger changes include:</p>
<ul>
<li><code>print</code> is now a function instead of a statement.  Keywords are now used to replace the special syntax of the old statement.  This change makes the language more consistent, but the most significant benefit is that it&#8217;s now substantially easier to alter the behavior of <em>print </em>in your code.  Here are a couple of examples:

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;foo&quot;</span> <span style="color: #808080; font-style: italic;">#old</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;foo&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#new</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #66cc66;">&gt;&gt;</span>openfile, <span style="color: #483d8b;">&quot;bar&quot;</span> <span style="color: #808080; font-style: italic;">#old</span>
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bar&quot;</span>, <span style="color: #008000;">file</span>=openfile<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#new</span></pre></div></div>

</li>
<li>String formatting has likewise received a significant overhaul.  Instead of using the old % syntax for templating strings, the <code>str</code> class now has a <code>format()</code> method.  Format can use positional arguments in the string ( <code>{0}</code>, <code>{1}</code> ) to pull values from a <code>list</code> or a <code>set</code>, but it can also use keywords to fetch values from dictionaries and objects (<code>{0[name]}</code>, <code>{sys.platform}</code> for <code>format(somedict)</code> and <code>format(sys=sys)</code> respectively).</li>
<li>All strings are Unicode now.  The <code>str</code> class has been replaced with the <code>unicode</code> class.  The <code>bytes</code> class has been introduced to store unencoded data.  Effectively this means that <code>str</code> behaves like <code>unicode</code> used to, and <code>bytes</code> behaves much like <code>str</code> used to.  The biggest difference in the new behavior is that you can now no longer mix the two types, and so you must use <code>str.encode()</code> and <code>bytes.decode()</code>.</li>
<li><code>long</code> has been renamed to <code>int</code>.  The old <code>int</code> type has been removed.  There is now only one integral type in python.</li>
<li>Integer division is now true division and returns a float.  <code>3/2</code> will return 1.5.  Use the <code>3//2</code> syntax to get the old behavior of returning 1.</li>
<li>String exceptions are dead.  All exceptions need to be exception classes that inherit from <code>BaseException</code> or it&#8217;s children.  (Though in most cases they should inherit from <code>Exception.</code>)  This means the long transition from Perl-style string exceptions to more Java-style object exceptions is complete.  (As a note, the object exceptions became even more Java-like with exception chaining being added.  The days of Python swallowing exceptions are numbered!)</li>
<li>Set literals have been added with syntax similar to dictionaries: <code>{1, 2}</code>.</li>
<li>Dictionaries and sets can now both be created with comprehensions.  A comprehension is like a <code>dict</code> or <code>set</code> literal that uses a function to populate the <code>set</code> or <code>dict</code> instead of declaring specific values.  Example:

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># set comprehension</span>
<span style="color: black;">&#123;</span>letter.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #483d8b;">&quot;foo&quot;</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #483d8b;">'F'</span>, <span style="color: #483d8b;">'O'</span><span style="color: black;">&#125;</span> <span style="color: #808080; font-style: italic;">#resulting set</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># dict comprehension</span>
<span style="color: black;">&#123;</span>letter: letter.<span style="color: black;">upper</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> letter <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #483d8b;">&quot;foo&quot;</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #483d8b;">'f'</span>: <span style="color: #483d8b;">'F'</span>, <span style="color: #483d8b;">'o'</span>: <span style="color: #483d8b;">'O'</span><span style="color: black;">&#125;</span> <span style="color: #808080; font-style: italic;">#resulting dict</span></pre></div></div>

</li>
<li>Views and Iterators replace lists in much of the standard library.  <code>dict.keys()</code>, <code>dict.items()</code>, <code>dict.values()</code>, <code>map()</code>, <code>filter()</code> and <code>zip()</code> amongst other functions no longer return lists.  Instead they return views or iterators.  This change prevents a whole group of possible hard to detect defects that come from people depending on the order of values returned from these functions.  Each of these more properly returns an unordered <code>set </code>or an <code>iterator</code> instead of a <code>list</code>.</li>
</ul>
<p>There are many other more minor changes as well, mostly involving the removal of parts of the language that should no longer be used such as old style classes. A few other highlights include the removal of <code>&lt;&gt;</code> in favor of <code>!=</code> for inequality, dropping <code>dict.has_key(foo)</code> in favor of <code>foo in dict</code> and the addition of function annotations to unify how various IDEs can do some level of type checking.</p>
<a class="wazi-ad-link" href="http://www.openlogic.com/open-source-support.php "><img width="590" height="75" src="http://olex.openlogic.com/wazi/wp-content/uploads/2010/05/open-source-support-main.png" class="attachment-maincontent_ad" alt="Get open source software support" title="Get open source software support" /></a>
<h3>Porting Your Code and the Value of Python 2.6</h3>
<p>Many of the changes for 3.0 have been previewed in the simultaneously developed release 2.6.  Version 2.6 is not just a new release in the 2.x series, it is also a fantastic tool for assisting in the migration of code from 2.x to 3.x.  Some changes &#8211; such as string formatting &#8211; were backported fully to 2.6, and many other features &#8211; such as the print function &#8211; are available to import from <code>__future__</code> for use in your 2.6 code today.  Finally, the tool 2to3 will automate most of the process of porting code from 2.6 to 3.0.  Reports so far are that most code can be made to work in 2.6 and automatically translated to 3.0 with a relative minimum of fuss.  As a note, the Python maintainers suggest that as long as a project wants to maintain both 2.x and 3.x sources, they should maintain the 2.x code, and use the code generated by 2to3 unmodified (with bugfixes to the 3.x code implemented in the 2.x sources).</p>
<h3>What Does It Mean?</h3>
<p>Python 3.0 has done a lot to keep the language&#8217;s syntax clear, clean and obvious, but that doesn&#8217;t mean anything if you don&#8217;t have the libraries you need.  Python 3.0 can be thought of as more of a transitional release then something targeted at the average developer.  For the average developer to find it useful, the libraries need to get ported first.  Until frameworks such as Django and libraries like Twisted are ported to the new syntax, likely most of us won&#8217;t find ourselves coding on a release past 2.6. Version 3.0 is a well done introduction to the new language syntax, but it&#8217;s really a release intended for the maintainers of popular Python libraries and frameworks so that they can get their offerings up and running for future releases like 3.1. or 3.2.  This should give the 3.0 code a chance to become rock solid and as fast as the 2.6 branch before we all make the leap (3.0 is purportedly about 10% slower then 2.6, but the C code is as yet unoptimized).  Python has taken a great step forward, but it will still be a year or two before the rest of us can take that step with it.</p>
<h3>Dig Deeper</h3>
<p>Find out about all the changes in the <a href="http://docs.python.org/dev/3.0/whatsnew/3.0.html">Python 3.0 changelog</a>.  You can also read about many of the transitional features in the <a href="http://docs.python.org/dev/2.6/whatsnew/2.6.html">Python 2.6 changelog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://olex.openlogic.com/wazi/2008/python-takes-a-great-step-forward-python-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
