Default Parameters Values and Mutable/Callable Objects

Quoting from the python Language Reference[link]:

Default parameter values are evaluated when the function definition is executed.
This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g. …

I was not aware of this. I have an excuse though, in previous versions of Python, language reference was described as “for language lawyers” [link]. Since I never liked lawyers, I never bothered reading it. I am happy though it was changed in current release and now it is described as “describes syntax and language elements”. I guess I have to read it now.

Below is an example for a bug I had caused by this feature and which made me aware of this issue.

>>> import time
>>> def now(t=time.time()):
...     print t
...
>>> now()
1228988224.36
>>> now()
1228988224.36

Installing Python 3.0 on Ubuntu 8.10 Intrepid Ibex

UPDATE (Dec, 7): apt-get install python3 works now. this might be the prefered way for most of us.

Based on sofen’s blog’s post about python 2.6

This was the easiest thing I did recently. It went so smooth that this post is totally redundant.

Seven commands and you are all unicode.

Step 1: Make sure you got all the prerequisites

$ sudo apt-get install build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdbm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev g++ gcc

Step 2: Grab the bits, extract them and build them

$ wget http://www.python.org/ftp/python/3.0/Python-3.0.tgz
$ tar xvzf Python-3.0.tgz
$ cd Python-3.0/
$ ./configure
$ make
$ sudo make install

That’s it my friend. You now have the best software development platform in the entire universe!

$ python3.0
Python 3.0 (r30:67503, Dec  4 2008, 21:27:40)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Good luck and enjoy the sweetness of open source software!