<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dw="https://www.dreamwidth.org">
  <id>tag:dreamwidth.org,2013-12-31:2137567</id>
  <title>Import That!</title>
  <subtitle>Steven D'Aprano</subtitle>
  <author>
    <name>Steven D'Aprano</name>
  </author>
  <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/"/>
  <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom"/>
  <updated>2015-02-22T14:19:15Z</updated>
  <dw:journal username="import_that" type="personal"/>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:6655</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/6655.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=6655"/>
    <title>A tale of yak shaving</title>
    <published>2014-07-26T08:35:14Z</published>
    <updated>2014-07-26T08:35:14Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">This is a couple of years old now, but still interesting: Barry Warsaw, one of the Python core developers, shares &lt;a href="http://www.wefearchange.org/2011/03/charming-snakes-and-shaving-yaks.html"&gt;the tale of an hairy debugging experience&lt;/a&gt; and the &lt;a href="http://en.wiktionary.org/wiki/yak_shaving"&gt;yak shaving&lt;/a&gt; needed to solve it:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;Everyone who reported the problem said the TypeError was getting thrown on the for-statement line. The exception message indicated that Python was getting some object that it was trying to convert to an integer, but was failing. How could you possible get that exception when either making a copy of a list or iterating over that copy? Was the list corrupted? Was it not actually a list but some list-like object that was somehow returning non-integers for its min and max indexes?&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=6655" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:4716</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/4716.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=4716"/>
    <title>Changing Python's prompt</title>
    <published>2014-05-02T03:30:03Z</published>
    <updated>2014-05-02T03:30:03Z</updated>
    <category term="startup"/>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">I have a mild dislike of Python's default prompt, &amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;quot;. Not that the prompt itself is bad, but when you copy from an interactive session and paste into email or a Usenet post, the prompt clashes with the standard &amp;gt; email quote marker. So I've changed my first level prompt to &amp;quot;py&amp;gt;&amp;quot; to distinguish interactive sessions from email quotes. Doing so is very simple:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;import sys
sys.ps1 = 'py&amp;gt; '&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Note the space at the end.&lt;br /&gt;&lt;br /&gt;You can change the second level prompt (by default, &amp;quot;...&amp;quot;) by assigning to &lt;code&gt;sys.ps2&lt;/code&gt;, but I haven't bothered. Both prompts &lt;a href="https://docs.python.org/2/library/sys.html#sys.ps1"&gt;support arbitrary objects&lt;/a&gt;, not just strings, which you can use to implement dynamic prompts similar to those iPython uses. Here's a simple example of numbering the prompts:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;class Prompt:
    def __init__(self):
        self.count = 0
    def __str__(self):
        self.count += 1
        return "[%4d] " % self.count

sys.ps1 = Prompt()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;If you're trying to use coloured prompts, there are some subtitles to be aware of. You have to escape any non-printing characters. See this &lt;a href="http://bugs.python.org/issue20359"&gt;bug report&lt;/a&gt; for details.&lt;br /&gt;&lt;br /&gt;You can have Python automatically use your custom prompt by setting it your startup file. If the environment variable &lt;code&gt;PYTHONSTARTUPFILE&lt;/code&gt; is set, Python will run the file named in that environment variable when you start the interactive interpreter. As I am using Linux for my desktop, I have the following line in my &lt;code&gt;.bashrc&lt;/code&gt; file to set the environment variable each time I log in:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;export PYTHONSTARTUP=/home/steve/python/startup.py&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and the startup file itself then sets the prompt, as shown above.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=4716" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:4367</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/4367.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=4367"/>
    <title>try-finally oddity</title>
    <published>2014-04-28T16:56:17Z</published>
    <updated>2014-04-30T11:50:50Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">There's a curious oddity with Python's treatment of &lt;code&gt;return&lt;/code&gt; inside &lt;code&gt;try...finally&lt;/code&gt; blocks:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;py&amp;gt; def test():
...     try:
...         return 23
...     finally:
...         return 42
...
py&amp;gt; test()
42
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;While it seems a little odd, I don't think we can really call it a &amp;quot;gotcha&amp;quot;, as it shouldn't be all that surprising. The &lt;code&gt;finally&lt;/code&gt; block is guaranteed to run when the &lt;code&gt;try&lt;/code&gt; block is left, however it is left. The first &lt;code&gt;return&lt;/code&gt; sets the return value to 23, the second resets it to 42.&lt;br /&gt;&lt;br /&gt;It should be no surprise that &lt;code&gt;finally&lt;/code&gt; can raise an exception:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;py&amp;gt; def test():
...     try: return 23
...     finally: raise ValueError
...
py&amp;gt; test()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 3, in test
ValueError
&lt;/stdin&gt;&lt;/module&gt;&lt;/stdin&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;A little less obvious is that it can also swallow exceptions:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;py&amp;gt; def test():
...     try: raise ValueError
...     finally: return 42
...
py&amp;gt; test()
42
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Earlier, I wrote that the &lt;code&gt;finally&lt;/code&gt; block is guaranteed to run. That's not &lt;em&gt;quite&lt;/em&gt; true. The &lt;code&gt;finally&lt;/code&gt; block won't run if control never leaves the &lt;code&gt;try&lt;/code&gt; block:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;try:
    while True:
        pass
finally:
    print('this never gets reached')
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;It also won't run if Python is killed by the operating system, e.g. in response to &lt;code&gt;kill -9&lt;/code&gt;, or following loss of power and other such catastrophic failures.&lt;br /&gt;&lt;br /&gt;There are also two official ways to force Python to exit without running cleanup code, including code in &lt;code&gt;finally&lt;/code&gt; blocks: &lt;a href="https://docs.python.org/3/library/os.html#os.abort"&gt;&lt;code&gt;os.abort&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://docs.python.org/3/library/os.html#os._exit"&gt;&lt;code&gt;os._exit&lt;/code&gt;&lt;/a&gt;. Both should be used only for specialized purposes. Normally you should exit your Python programs by:&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Falling out the bottom of the program. When there is no more code to run, Python exits cleanly.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Calling &lt;code&gt;sys.exit&lt;/code&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Raising &lt;code&gt;SystemExit&lt;/code&gt;.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=4367" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:3098</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/3098.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=3098"/>
    <title>Classic and new classes</title>
    <published>2014-04-22T16:20:45Z</published>
    <updated>2014-04-22T16:20:45Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">One of the historical oddities of Python is that there are two different kinds of classes, so-called &amp;quot;classic&amp;quot; or &amp;quot;old-style&amp;quot; classes, and &amp;quot;new-style&amp;quot; classes or types. To understand the reason, we have to go back to the dawn of time and very early Python.&lt;br /&gt;&lt;br /&gt;Back in the early Python 1.x days, built-in types like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;list&lt;/code&gt; were completely separate from classes you created with the &lt;code&gt;class&lt;/code&gt; keyword. Built-in types were written in C, classes were written in Python, and the two could not be combined. So in Python 1.5, you couldn't inherit from built-in types:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;&amp;gt;&amp;gt;&amp;gt; class MyInt(int):
...     pass
...
Traceback (innermost last):
  File "&lt;stdin&gt;", line 1, in ?
TypeError: base is not a class object&lt;/stdin&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;If you wanted to inherit behaviour from a built-in type, the only way to do so was by using &lt;a href="https://en.wikipedia.org/wiki/Delegation_%28programming%29"&gt;delegation&lt;/a&gt;, a powerful and useful technique that is unfortunately underused today. At the time though, it was the only way to solve the problem of subclassing from built-in types, leading to useful recipes like &lt;a href="http://code.activestate.com/recipes/52295/"&gt;this one from Alex Martelli&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;But in Python 2.2, &lt;a href="https://www.python.org/download/releases/2.2.3/descrintro"&gt;classes were unified with built-in types&lt;/a&gt;. This involved a surprising number of changes to the behaviour of classes, so for backwards-compatibility, both the old and the new behaviour was kept. Rather than introduce a new keyword, Python 2.2 used a simple set of rules:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;If you define a class that doesn't inherit from anything, it is an old-style classic class.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;If your class inherits from another classic class, it too is a classic class.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;But if you inherit from a built-in type (e.g. &lt;code&gt;dict&lt;/code&gt; or &lt;code&gt;list&lt;/code&gt;) then it is a new-style class and the new rules apply.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Since Python supports &lt;a href="https://en.wikipedia.org/wiki/Multiple_inheritance"&gt;multiple inheritance&lt;/a&gt;, you might inherit from both new-style and old-style classes; in that case, your class will be new-style.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;To aid with this, a new built-in type was created, &lt;code&gt;object&lt;/code&gt;. &lt;code&gt;object&lt;/code&gt; is the parent type of all new-style classes and types, but not old-style classes. Curiously, &lt;code&gt;isinstance(classic_instance, object)&lt;/code&gt; still returns True, even though &lt;code&gt;classic_instance&lt;/code&gt; doesn't actually inherit from &lt;code&gt;object&lt;/code&gt;. At least that is the case in Python 2.4 and above, I haven't tested 2.2 or 2.3.&lt;br /&gt;&lt;br /&gt;There are a few technical differences in behaviour between the old- and new-style classes, including:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;super&lt;/code&gt; only works in new-style classes.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Likewise for &lt;code&gt;classmethod&lt;/code&gt; and &lt;code&gt;staticmethod&lt;/code&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="https://docs.python.org/2/library/functions.html#property"&gt;Properties&lt;/a&gt; &lt;em&gt;appear&lt;/em&gt; to work in classic classes, but actually don't. They seem to work so long as you only read from them, but if you assign to a property, the setter method is not called and the property is replaced.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;In general, &lt;a href="https://docs.python.org/2/reference/datamodel.html#descriptors"&gt;descriptors&lt;/a&gt; of any sort only work with new-style classes.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;New-style classes support &lt;a href="https://docs.python.org/2/reference/datamodel.html#__slots__"&gt;&lt;code&gt;__slots__&lt;/code&gt;&lt;/a&gt; as a memory optimization.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;The rules for resolving inheritance of old- and new-style classes are slightly different, with old-style classes' &lt;a href="https://docs.python.org/2/glossary.html#term-method-resolution-order"&gt;method resolution order&lt;/a&gt; being inconsistent under certain circumstances.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;New-style classes optimize operator overloading by skipping dunder methods like &lt;code&gt;__add__&lt;/code&gt; which are defined on the instance rather than the class.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;The complexity of having two kinds of class was always intended to be a temporary measure, and in Python 3, classic classes were finally removed for good. Now, &lt;em&gt;all&lt;/em&gt; classes inherit from &lt;code&gt;object&lt;/code&gt;, even if you just write &lt;code&gt;class MyClass:&lt;/code&gt; with no explicit bases.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=3098" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:2873</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/2873.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=2873"/>
    <title>More on language popularity</title>
    <published>2014-04-21T05:33:14Z</published>
    <updated>2014-04-21T05:33:14Z</updated>
    <category term="python"/>
    <category term="popularity"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Recently, I wrote about &lt;a href="http://import-that.dreamwidth.org/1388.html"&gt;the various ways of measuring language popularity&lt;/a&gt;, and I thought I'd add another two.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://trendyskills.com/"&gt;TrendySkills&lt;/a&gt; measures popularity of IT technologies (not just languages) by extracting information from job advertisements. It's currently showing Python at number 9, just ahead of C and just behind HTML5. Despite the name, the site doesn't appear to measure &lt;em&gt;trends&lt;/em&gt; as such (what technologies are becoming more popular or less popular), but only snapshots of current popularity. It also mixes data collected from &lt;a href="http://trendyskills.com/about"&gt;numerous countries&lt;/a&gt;, including the USA, Spain and Sweden. I don't think the job market is truly world-wide, not even in IT, so that seems a weakness to me: just because a technology is popular in one country doesn't mean it will be equally popular in another.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://redmonk.com/sogrady/2014/01/22/language-rankings-1-14/"&gt;RedMonk&lt;/a&gt; periodically posts a graph of language popularity based on GitHub and StackOverflow. They find Python in position 5, sandwiched between C# and C++.&lt;br /&gt;&lt;br /&gt;However you measure it, there's no doubt that Python is one of the most popular and influential languages around.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=2873" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:2662</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/2662.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=2662"/>
    <title>Another 2.8 proposal</title>
    <published>2014-04-15T03:24:57Z</published>
    <updated>2014-07-26T07:30:34Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">There's yet another call for Python 2.8, or at least for discussion for 2.8. Martijn Faassen &lt;a href="http://blog.startifact.com/posts/the-call-of-python-28.html"&gt;discusses what Python 2.8 should be&lt;/a&gt;, even though it won't be. Frankly, I'm not entirely sure what motivates Martijn to write this post &amp;mdash; he seems to have accepted that &lt;a href="http://python.org/dev/peps/pep-0404/"&gt;there won't be a Python 2.8&lt;/a&gt; and isn't asking for that decision to be rethought., so I'm not entirely sure why he wants to discuss 2.8, but let's treat this as a serious suggestion.&lt;br /&gt;&lt;br /&gt;Martijn suggests that, paradoxically, the best way to handle the Python 2.x to 3.x transition is for 2.8 to break backwards compatibility with 2.7. I thought we had that version. Isn't it called Python 3.x? Not according to Martijn, who wants to add &amp;mdash; or rather since he explicitly says he won't be doing the work, he wants somebody else to add &amp;mdash; a whole series of extra compiler options in the form of &lt;code&gt;from __future3__&lt;/code&gt; and &lt;code&gt;from __past__&lt;/code&gt; imports. Like &lt;a href="http://python.org/dev/peps/pep-0236/"&gt;&lt;code&gt;__future__&lt;/code&gt;&lt;/a&gt;, they will presumably behave like compiler directives and change the behaviour of Python.&lt;br /&gt;&lt;span class="cut-wrapper"&gt;&lt;span style="display: none;" id="span-cuttag___1" class="cuttag"&gt;&lt;/span&gt;&lt;b class="cut-open"&gt;(&amp;nbsp;&lt;/b&gt;&lt;b class="cut-text"&gt;&lt;a href="https://import-that.dreamwidth.org/2662.html#cutid1"&gt;Read more...&lt;/a&gt;&lt;/b&gt;&lt;b class="cut-close"&gt;&amp;nbsp;)&lt;/b&gt;&lt;/span&gt;&lt;div style="display: none;" id="div-cuttag___1" aria-live="assertive"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=2662" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:2364</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/2364.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=2364"/>
    <title>More on variance</title>
    <published>2014-04-09T14:25:03Z</published>
    <updated>2014-07-26T07:34:12Z</updated>
    <category term="python"/>
    <category term="statistics"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Earlier, &lt;a href="http://import-that.dreamwidth.org/2291.html"&gt;I discussed&lt;/a&gt; some of the terminology and different uses for the two variance functions in Python 3.4's new &lt;a href="http://docs.python.org/3.4/library/statistics.html"&gt;statistics&lt;/a&gt; module. That was partly motivated by a question from Andrew Szeto, who asked me why the variance functions took optional &lt;code&gt;mu&lt;/code&gt; or &lt;code&gt;xbar&lt;/code&gt; parameters.&lt;br /&gt;&lt;br /&gt;The two standard deviation functions &lt;code&gt;stdev&lt;/code&gt; and &lt;code&gt;pstdev&lt;/code&gt; are just thin wrappers that return the square root of the variance, so they have the same signature as the variance functions.&lt;br /&gt;&lt;br /&gt;I had a few motives for including the second parameter, in no particular order:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;ol&gt;&lt;li&gt;The reason given in the PEP was that I took the idea from the &lt;a href="https://www.gnu.org/software/gsl/manual/html_node/Mean-and-standard-deviation-and-variance.html"&gt;GNU Scientific Library&lt;/a&gt;. Perhaps they know something I don't? (I actually thought of the idea independently, but when I was writing &lt;a href="https://www.python.org/dev/peps/pep-0450/"&gt;PEP 450&lt;/a&gt; I expected this to be controversial, and was pleased to find prior art.)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;It allows a neat micro-optimization to avoid having to recalculate the mean if you've already calculated it. If you have a large data set, or one with custom numeric types where the &lt;code&gt;__add__&lt;/code&gt; method is expensive, calculating the mean once instead of twice may save some time.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Mathematically, the variance is in &lt;a href="http://mathworld.wolfram.com/Parameter.html"&gt;some sense&lt;/a&gt; a function dependent on μ (mu) or x̄ (xbar). Making them parameters of the Python functions reflects that sense.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Variance has a &lt;a href="http://www.math.uah.edu/stat/expect/Variance.html"&gt;nice interpretation in physics&lt;/a&gt;: it's the moment of inertia around the centre of mass. We can calculate the moment of inertia around any point, not just the centre &amp;mdash; might we not also calculate the &amp;quot;variance&amp;quot; around some point other than the mean? If you want to abuse the variance function by passing (say) the median instead of the mean as &lt;code&gt;xbar&lt;/code&gt; or &lt;code&gt;mu&lt;/code&gt;, you can. But if you do, you're responsible for ensuring that the result is physically meaningful. Don't come complaining to me if you get a negative variance or some other wacky value.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;It also allows you to calculate an improved sample variance by passing the known population mean to the &lt;code&gt;pvariance&lt;/code&gt; function &amp;mdash; see &lt;a href="http://import-that.dreamwidth.org/2291.html"&gt;my earlier post&lt;/a&gt; or &lt;a href="https://en.wikipedia.org/wiki/Bessel%27s_correction"&gt;Wikipedia&lt;/a&gt; for details.&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Individually, none of theses were especially strong, and probably wouldn't have justified the additional complexity on their own. But taken together I think they justified including the optional parameters.&lt;br /&gt;&lt;br /&gt;Surprisingly (at least to me), I don't recall much if any opposition to these mu/xbar parameters. I expected this feature would be a lot more controversial than it turned out to be. If I recall correctly, there was more bike-shedding about what to call the parameters (I initially just called them &amp;quot;m&amp;quot;, for mu/mean) than whether or not to include them.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=2364" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:2291</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/2291.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=2291"/>
    <title>Population and sample variance</title>
    <published>2014-03-21T12:34:56Z</published>
    <updated>2014-07-26T07:33:10Z</updated>
    <category term="python"/>
    <category term="statistics"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">I am the author of &lt;a href="http://python.org/dev/peps/pep-0450/"&gt;PEP 450&lt;/a&gt; and &lt;a href="http://docs.python.org/3.4/library/statistics.html"&gt;the statistics module&lt;/a&gt;. That module offers four different functions related to &lt;a href="https://en.wikipedia.org/wiki/Variance"&gt;statistical variance&lt;/a&gt;, and some people may not quite understand what the difference between them.&lt;br /&gt;&lt;br /&gt;[Disclaimer: statistical variance is complicated, and my discussion here is quite simplified. In particular, most of what I say only applies to &amp;quot;reasonable&amp;quot; data sets which aren't too skewed or &lt;a href="https://en.wikipedia.org/wiki/Cauchy_distribution"&gt;unusual&lt;/a&gt;, and samples which are random and representative. If your sample data is not representative of the population from which it is drawn, then all bets are off.]&lt;br /&gt;&lt;br /&gt;The statistics module offers two variance functions, &lt;code&gt;pvariance&lt;/code&gt; and &lt;code&gt;variance&lt;/code&gt;, and two corresponding versions of the standard deviation, &lt;code&gt;pstdev&lt;/code&gt; and &lt;code&gt;stdev&lt;/code&gt;. The standard deviation functions are just thin wrappers which take the square root of the appropriate variance function, so there's not a lot to say about them. Except where noted differently, everything I say about the &lt;code&gt;(p)variance&lt;/code&gt; functions also applies to the &lt;code&gt;(p)stdev&lt;/code&gt; functions, so for brevity I will only talk about variance.&lt;br /&gt;&lt;br /&gt;The two versions of variance give obviously different results:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;py&amp;gt; import statistics
py&amp;gt; data = [1, 2, 3, 3, 3, 5, 8]
py&amp;gt; statistics.pvariance(data)
4.53061224489796
py&amp;gt; statistics.variance(data)
5.2857142857142865&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;So which should you use? In a nutshell, two simple rules apply:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;ul&gt;&lt;li&gt;If you are dealing with the entire &lt;a href="https://en.wikipedia.org/wiki/Statistical_population"&gt;population&lt;/a&gt;, use &lt;code&gt;pvariance&lt;/code&gt;.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;If you are working with a &lt;a href="https://en.wikipedia.org/wiki/Sample_(statistics)"&gt;sample&lt;/a&gt;, use &lt;code&gt;variance&lt;/code&gt; instead.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;If you remember those two rules, you won't go badly wrong. Or at least, no more badly than most naive users of statistical functions. You want to be better than them, don't you? Then read on...&lt;br /&gt;&lt;br /&gt;&lt;span class="cut-wrapper"&gt;&lt;span style="display: none;" id="span-cuttag___1" class="cuttag"&gt;&lt;/span&gt;&lt;b class="cut-open"&gt;(&amp;nbsp;&lt;/b&gt;&lt;b class="cut-text"&gt;&lt;a href="https://import-that.dreamwidth.org/2291.html#cutid1"&gt;Read more...&lt;/a&gt;&lt;/b&gt;&lt;b class="cut-close"&gt;&amp;nbsp;)&lt;/b&gt;&lt;/span&gt;&lt;div style="display: none;" id="div-cuttag___1" aria-live="assertive"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=2291" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:1634</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/1634.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=1634"/>
    <title>Bike-shedding</title>
    <published>2014-03-21T02:31:39Z</published>
    <updated>2014-03-21T03:35:23Z</updated>
    <category term="quotes"/>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Nathaniel Smith describes the &lt;a href="https://mail.python.org/pipermail/python-ideas/2014-March/027213.html"&gt;culture&lt;/a&gt; of the &lt;a href="https://mail.python.org/mailman/listinfo/python-ideas"&gt;python-ideas&lt;/a&gt; and &lt;a href="https://mail.python.org/mailman/listinfo/python-dev"&gt;python-dev&lt;/a&gt; mailing lists:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;We're more of the love, bikeshedding, and rhetoric school. Well, we can do you bikeshedding and love without the rhetoric, and we can do you bikeshedding and rhetoric without the love, and we can do you all three concurrent or consecutive. But we can't give you love and rhetoric without the bikeshedding. Bikeshedding is compulsory.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="https://en.wikipedia.org/wiki/Parkinson%27s_law_of_triviality"&gt;Bike-shedding&lt;/a&gt; gets a &lt;a href="http://bikeshed.com/"&gt;bad rap&lt;/a&gt;, and deservedly so. But bike-shedding can also be a sign of passion and attention to detail. Sometimes it &lt;em&gt;really does&lt;/em&gt; matter what colour the bike-shed is: &amp;quot;colour&amp;quot; (syntax) can have functional and practical consequences, even for real-life bike-sheds. Dark colours tend to absorb and retain more heat than light colours. Syntax matters. Languages which feel like a harmonious whole &amp;mdash; even if the design sometimes &lt;a href="https://www.python.org/dev/peps/pep-0020/"&gt;only makes sense if you are Dutch&lt;/a&gt; &amp;mdash; require that the designers care about the little details of syntax and spelling. Even though functionally there would be little difference, Python would be a very different language indeed if we spelled this:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;def func(x, y):
    return x/(x+y)
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;as this: &lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;: func OVER + / ;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;It would in fact be &lt;a href="https://en.wikipedia.org/wiki/Forth_%28programming_language%29"&gt;Forth&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So let's hear it for a little bit of bike-shedding &amp;mdash; &lt;em&gt;but not too much!&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=1634" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:1388</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/1388.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=1388"/>
    <title>Language popularity</title>
    <published>2014-03-05T00:54:31Z</published>
    <updated>2015-02-22T14:19:15Z</updated>
    <category term="python"/>
    <category term="popularity"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">What's the most popular programming language in the world? Or at least those parts of the English-speaking world which are easily found on the Internet? C, C++, Java, PHP, Javascript, VB? Is &lt;a href="http://www.python.org/"&gt;Python&lt;/a&gt; on the rise on in decline? How do we know?&lt;br /&gt;&lt;br /&gt;There are a few websites which make the attempt to measure programming language popularity, for some definition of &amp;quot;popularity&amp;quot;. Since they all have different methods of measuring popularity, and choose different proxies to measure (things like the number of job ads or the number of on-line tutorials for a language), they give different results &amp;mdash; sometimes quite radically different, which is a strong indicator that even if language popularity has a single objective definition (and it probably doesn't) none of these methods are measuring it.&lt;br /&gt;&lt;br /&gt;So keeping in mind that any discussion of language popularity should be taken with a considerable pinch of salt, let's have a look at four well-known sites that try to measure popularity.&lt;br /&gt;&lt;br /&gt;&lt;span class="cut-wrapper"&gt;&lt;span style="display: none;" id="span-cuttag___1" class="cuttag"&gt;&lt;/span&gt;&lt;b class="cut-open"&gt;(&amp;nbsp;&lt;/b&gt;&lt;b class="cut-text"&gt;&lt;a href="https://import-that.dreamwidth.org/1388.html#cutid1"&gt;Read more...&lt;/a&gt;&lt;/b&gt;&lt;b class="cut-close"&gt;&amp;nbsp;)&lt;/b&gt;&lt;/span&gt;&lt;div style="display: none;" id="div-cuttag___1" aria-live="assertive"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=1388" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:1130</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/1130.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=1130"/>
    <title>Does Python pass by reference or value?</title>
    <published>2014-03-01T16:38:40Z</published>
    <updated>2014-03-01T16:38:40Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">One topic which comes up from time to time, usually generating a lot of heat and not much light, is the question of how Python passes values to functions and methods. Usually the question is posed as &amp;quot;Does Python use pass-by-reference or pass-by-value?&amp;quot;.&lt;br /&gt;&lt;br /&gt;The answer is, &lt;strong&gt;neither&lt;/strong&gt;. Python, like most modern object-oriented languages, uses an argument passing strategy first named by one of the pioneers of object-oriented programming, &lt;a href="https://en.wikipedia.org/wiki/Barbara_Liskov"&gt;Barbara Liskov&lt;/a&gt;, in 1974 for the language &lt;a href="http://en.wikipedia.org/wiki/CLU_(programming_language)"&gt;CLU&lt;/a&gt;. Liskov named it &lt;em&gt;pass-by-object-sharing&lt;/em&gt;, or just &lt;em&gt;pass-by-sharing&lt;/em&gt;, but the actual strategy is a lot older, being the same as how Lisp passes arguments. Despite this august background, most people outside of Python circles have never heard of pass-by-sharing, and consequently there is a lot of confusion about argument passing terminology.&lt;br /&gt;&lt;br /&gt;Let's start by looking at how people get confused. Let's start by &amp;quot;proving&amp;quot; that Python is pass-by-value (also know as call-by-value), then we'll &amp;quot;prove&amp;quot; that Python is pass-by-reference (call-by-reference). It's actually neither, but if you think that there are only two ways to pass arguments to a function, you might be fooled into thinking Python uses both. &lt;span class="cut-wrapper"&gt;&lt;span style="display: none;" id="span-cuttag___1" class="cuttag"&gt;&lt;/span&gt;&lt;b class="cut-open"&gt;(&amp;nbsp;&lt;/b&gt;&lt;b class="cut-text"&gt;&lt;a href="https://import-that.dreamwidth.org/1130.html#cutid1"&gt;Read more...&lt;/a&gt;&lt;/b&gt;&lt;b class="cut-close"&gt;&amp;nbsp;)&lt;/b&gt;&lt;/span&gt;&lt;div style="display: none;" id="div-cuttag___1" aria-live="assertive"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=1130" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:956</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/956.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=956"/>
    <title>Lies in code</title>
    <published>2014-03-01T12:19:57Z</published>
    <updated>2014-03-01T12:24:21Z</updated>
    <category term="quotes"/>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Quote of the week:&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'.&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;-- Michael Foord &lt;a href="https://mail.python.org/pipermail/python-dev/2009-March/087396.html"&gt;paraphrases Christian Muirhead on python-dev&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=956" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
  <entry>
    <id>tag:dreamwidth.org,2013-12-31:2137567:676</id>
    <link rel="alternate" type="text/html" href="https://import-that.dreamwidth.org/676.html"/>
    <link rel="self" type="text/xml" href="https://import-that.dreamwidth.org/data/atom/?itemid=676"/>
    <title>When to use assert</title>
    <published>2014-03-01T08:26:57Z</published>
    <updated>2014-03-01T16:40:46Z</updated>
    <category term="python"/>
    <dw:security>public</dw:security>
    <dw:reply-count>0</dw:reply-count>
    <content type="html">Python's &lt;a href="http://docs.python.org/3/reference/simple_stmts.html#the-assert-statement"&gt;&lt;code&gt;assert&lt;/code&gt; statement&lt;/a&gt; is a very useful feature that unfortunately often gets misused. &lt;code&gt;assert&lt;/code&gt; takes an expression and an optional error message, evaluates the expression, and if it gives a true value, does nothing. If the expression evaluates to a false value, it raises an &lt;code&gt;AssertionError&lt;/code&gt; exception with optional error message. For example:&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-left: 40px;"&gt;&lt;pre&gt;
py&amp;gt; x = 23
py&amp;gt; assert x &amp;gt; 0, &amp;quot;x is zero or negative&amp;quot;
py&amp;gt; assert x%2 == 0, &amp;quot;x is an odd number&amp;quot;
Traceback (most recent call last):
  File &amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;
AssertionError: x is an odd number
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Many people use assertions as a quick and easy way to raise an exception if an argument is given the wrong value. But this is wrong, badly wrong, for two reasons. &lt;span class="cut-wrapper"&gt;&lt;span style="display: none;" id="span-cuttag___1" class="cuttag"&gt;&lt;/span&gt;&lt;b class="cut-open"&gt;(&amp;nbsp;&lt;/b&gt;&lt;b class="cut-text"&gt;&lt;a href="https://import-that.dreamwidth.org/676.html#cutid1"&gt;Read more...&lt;/a&gt;&lt;/b&gt;&lt;b class="cut-close"&gt;&amp;nbsp;)&lt;/b&gt;&lt;/span&gt;&lt;div style="display: none;" id="div-cuttag___1" aria-live="assertive"&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="https://www.dreamwidth.org/tools/commentcount?user=import_that&amp;ditemid=676" width="30" height="12" alt="comment count unavailable" style="vertical-align: middle;"/&gt; comments</content>
  </entry>
</feed>
