<?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>» DoYourself.org &#187; Python</title>
	<atom:link href="http://www.doyourself.org/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.doyourself.org</link>
	<description>Did you know ?</description>
	<lastBuildDate>Fri, 03 Feb 2012 23:00:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python and encapsulation</title>
		<link>http://www.doyourself.org/python/68-python-and-encapsulation/</link>
		<comments>http://www.doyourself.org/python/68-python-and-encapsulation/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[encapsulation]]></category>

		<guid isPermaLink="false">http://www.doyourself.org/?p=68</guid>
		<description><![CDATA[The Python language contains the OO concept of a class. Rather strangely, it only really does half the job in that it doesn&#8217;t provide a way to properly encapsulate your class&#8217; members. Take this Java class, for example: class Steve { private String name; public String getName() { return name; } public void setName( final [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The Python language contains the OO concept of a class. Rather strangely, it only really does half the job in that it doesn&#8217;t provide a way to properly encapsulate your class&#8217; members.</p>
<p>Take this Java class, for example:</p>
<pre>
class Steve
{
    private String name;    

    public String getName()
    {
        return name;
    }

    public void setName( final String n )
    {
        name = n;
    }
}</pre>
<p>This class is properly encapsulated. The getter and setter control the values that can be placed into, and read from, the name property.</p>
<p>You can&#8217;t do that with Python, you can create a similar looking class, like so:</p>
<pre>
class Steve:
  name = "";
  def getName(self):
    return self.name;
  def setName( self, n )
    self.name = n;</pre>
<p>But there is no way to make the name variable private &#8211; it is always public. You <em>can</em> define a variable with a leading double-underscore, like so:</p>
<pre>
class Steve:
  __name = "";
  def getName(self):
    return self.__name;
  def setName( self, n ):
    self.__name = n;</pre>
<p>This stops consumers from using Steve().name directly but __name is still not private &#8211; it is merely decorated differently. But it can still be obtained directly by using Steve()._Steve__name instead.</p>
<p>I&#8217;m not sure if the intention is to update this in a future version of Python &#8211; Python3000 breaks backwards compatibility so this is something that could go in. I hope it does get fixed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doyourself.org/python/68-python-and-encapsulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python PDF tools</title>
		<link>http://www.doyourself.org/python/69-python-pdf-tools/</link>
		<comments>http://www.doyourself.org/python/69-python-pdf-tools/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.doyourself.org/?p=69</guid>
		<description><![CDATA[I&#8217;ve been using iText &#8211; the Java PDF library &#8211; a lot recently and seeing as I&#8217;ve been getting into Python I wanted something similar to iText for that. I managed to dig these up: ReportLab -Â  A PDF creation tool in the iText mould but the ReportLab site hasn&#8217;t been updated since 2005 so [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I&#8217;ve been using iText &#8211; the Java PDF library &#8211; a lot recently and seeing as I&#8217;ve been getting into Python I wanted something similar to iText for that. I managed to dig these up:</p>
<p><a href="http://www.reportlab.org/rl_toolkit.html">ReportLab</a> -Â  A PDF creation tool in the iText mould but the ReportLab site hasn&#8217;t been updated since 2005 so looks like this is a dead project.</p>
<p><a href="http://pybrary.net/pyPdf/">PyPDF</a> -Â  A simple library that lets you change PDF metadata (author, title, etc) and to crop, join or split PDF files. PyPDF doesn&#8217;t have the ability to modify a page&#8217;s content.</p>
<p><a href="http://www.htmltopdf.org/demo.html">HTML2PDF</a> -Â  A the name suggests, this takes some HTML input and creates a PDF from it. Looks good.</p>
<p>Another option may be to compile iText using the <a href="http://gcc.gnu.org/java/index.html">GNU Java compiler</a> and then to create a Python wrapper for it. I&#8217;ve never used GCJ so I don&#8217;t know if it would even compile iText cleanly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doyourself.org/python/69-python-pdf-tools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reverse a string using Python slices</title>
		<link>http://www.doyourself.org/python/71-reverse-a-string-using-python-slices/</link>
		<comments>http://www.doyourself.org/python/71-reverse-a-string-using-python-slices/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[a]]></category>
		<category><![CDATA[Reverse]]></category>
		<category><![CDATA[slices]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[using]]></category>

		<guid isPermaLink="false">http://www.doyourself.org/?p=71</guid>
		<description><![CDATA[I looked through the Python String Methods page expecting to see a Reverse() function but there isn&#8217;t one. Googling further I found plenty of people suggesting using this: s = s[::-1] But what the hell is that? Not the most intuitive syntax I&#8217;ve ever seen. This is using the Python Slice syntax. Python can take [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>			I looked through the <a href="http://www.python.org/doc/2.5.2/lib/string-methods.html" target="_self">Python String Methods</a> page expecting to see a Reverse() function but there isn&#8217;t one. Googling further I found plenty of people suggesting using this:</p>
<blockquote><p>s = s[::-1]</p>
</blockquote>
<p>But what the hell is that? Not the most intuitive syntax I&#8217;ve ever seen. This is using the <a href="http://www.python.org/doc/2.3.5/whatsnew/section-slices.html" target="_self">Python Slice</a> syntax.</p>
<p>Python can take a slice from a list or a string. The syntax is similar to array accessor in other languages like C, C++ and Pascal &#8211; in those languages you could do:</p>
<blockquote><p>char c = s[3];</p>
</blockquote>
<p>And in Python it&#8217;s the same &#8211; but this has been taken further and that array accessor is now much more powerful. For example:</p>
<blockquote><p>s = &#8220;hello world&#8221;<br />
print s[6:11]<br />
&gt;&gt;world</p>
<p>l = [0,1,2,3,4,5,6,7,8,9]<br />
print l[2]<br />
&gt;&gt;2</p>
</blockquote>
<p>So it can work like a SubString() function in the first example, like a standard array accessor in the second.</p>
<p>The extended slices add a third parameter, which is the step argument &#8211; which basically means it&#8217;s the number of elements to jump before retrieving the next element. For example</p>
<blockquote><p>l = [0,1,2,3,4,5,6,7,8,9]<br />
print l[0:9:2]<br />
&gt;&gt;[0, 2, 4, 6, 8]</p>
<p>l = [0,1,2,3,4,5,6,7,8,9]<br />
print l[::2]<br />
&gt;&gt;[0, 2, 4, 6, 8]</p>
</blockquote>
<p>Notice the two different slice syntaxes above giving the same output &#8211; [0:9:2] and [::2]. If you leave the first parameter blank it defaults to 0 and if you leave the second blank it defaults to the length of the variable being sliced.</p>
<p>And finally by putting -1 in the step you get the reverse string behaviour</p>
<blockquote><p>s = &#8220;hello world&#8221;<br />
print s[::-1]<br />
dlrow olleh</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.doyourself.org/python/71-reverse-a-string-using-python-slices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Python, The Print and The Comma</title>
		<link>http://www.doyourself.org/python/88-the-python-the-print-and-the-comma/</link>
		<comments>http://www.doyourself.org/python/88-the-python-the-print-and-the-comma/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[and]]></category>
		<category><![CDATA[Comma]]></category>
		<category><![CDATA[Print]]></category>
		<category><![CDATA[the]]></category>

		<guid isPermaLink="false">http://www.doyourself.org/?p=88</guid>
		<description><![CDATA[On the Modules page of the Python docs, there is this code example: def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b &#60; n: print b, a, b = b, a+b Being new to Python, this code snippet confused me for three reasons: What&#8217;s the comma at the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>On the Modules page of the Python docs, there is this code example:</p>
<pre>
def fib(n):    # write Fibonacci series up to n

  a, b = 0, 1

  while b &lt; n:

    print b,

    a, b = b, a+b</pre>
<p>Being new to Python, this code snippet confused me for three reasons:</p>
<ul>
<li>What&#8217;s the comma at the end of the print statement for?</li>
<li>Is the &#8220;a, b = b, a+b&#8221; line a continuation of the print line?</li>
<li>And finally, is the &#8220;a, b = b, a+b&#8221; line basically three assignments separated by commas?</li>
</ul>
<p>I knew that when given an input parameter of 1000 the Fibonacci functions outputs:</p>
<pre>1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</pre>
<p>Which basically meant that my assumptions above were all wrong.</p>
<p><strong>Commas at the end of Print statements</strong></p>
<p>In Python, the default behavior of Print is to add a newline character at the end of the printed text &#8211; the only time the newline is not added is if a print string ends with a comma. For example:</p>
<pre>
print "line one"
print "line two"
print "line three",
print "line four"</pre>
<p>Outputs:</p>
<pre>
line one
line two
line three line four</pre>
<p>One subtle thing to notice with that output is the space between &#8220;line three&#8221; and &#8220;line four&#8221;. <em>Ending a Print with a comma adds a space</em>.</p>
<p><strong>Commas in assignments</strong></p>
<p>My first reaction was to read this line &#8220;a, b = b, a+b&#8221; as meaning <em>perform three separate assignments. </em>Clearly this couldn&#8217;t be because the &#8220;a&#8221; variable was not being assigned in any way and the known function output wouldn&#8217;t match that behaviour. What it is actually doing is assigning the values from list &#8220;b, a+b&#8221; to list &#8220;a, b&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doyourself.org/python/88-the-python-the-print-and-the-comma/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Zen of Python</title>
		<link>http://www.doyourself.org/python/90-the-zen-of-python/</link>
		<comments>http://www.doyourself.org/python/90-the-zen-of-python/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 19:57:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[of]]></category>
		<category><![CDATA[the]]></category>
		<category><![CDATA[Zen]]></category>

		<guid isPermaLink="false">http://www.doyourself.org/?p=90</guid>
		<description><![CDATA[Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren&#8217;t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>			Beautiful is better than ugly.<br />
Explicit is better than implicit.<br />
Simple is better than complex.<br />
Complex is better than complicated.<br />
Flat is better than nested.<br />
Sparse is better than dense.<br />
Readability counts.<br />
Special cases aren&#8217;t special enough to break the rules.<br />
Although practicality beats purity.<br />
Errors should never pass silently.<br />
Unless explicitly silenced.<br />
In the face of ambiguity, refuse the temptation to guess.<br />
There should be one&#8211; and preferably only one &#8211;obvious way to do it.<br />
Although that way may not be obvious at first unless you&#8217;re Dutch.<br />
Now is better than never.<br />
Although never is often better than *right* now.<br />
If the implementation is hard to explain, it&#8217;s a bad idea.<br />
If the implementation is easy to explain, it may be a good idea.<br />
Namespaces are one honking great idea &#8212; let&#8217;s do more of those!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.doyourself.org/python/90-the-zen-of-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

