From: Bruce S. <ba...@an...> - 2001-11-19 16:23:38
|
I should mention that there are some interesting new features in Python 2.2, and you can read about them at http://www.python.org. Of particular interest to those of us doing scientific computations is the following example: from __future__ import division # two underscores before and after "future" print 3/2 The result is 1.5, whereas without the "from" statement the result is 1. The intent is that with Python 3.0, some time in the future, this behavior of division will be standard, but you can import this behavior "from the future" right now. Also as of Python 2.2 if you want the old "integer division" behavior you can us two slashes: 3//2 gives 1. People are encouraged slowly to change to the new syntax. There will be a warning mechanism you can invoke that will tell you of any existing integer division cases, so you can decide whether to replace "/" with "//". This change to Python was prompted in part by observations that novice programmers often make the mistake of writing 3/2 when they thought they would get the behavior of 3.0/2.0. This was one of the few significant problems our own physics students have had with Python syntax, and it's often hard to track down the source of the problem when a program malfunctions due to this. Frankly, it's a mistake that experienced programmers also make sometimes when doing scientific computations. Bruce Sherwood |