[AstroPy] Practical Python for Astronomers web tutorial
sransom at nrao.edu
Wed May 25 11:16:15 EDT 2011
I'm going to chime in here... I've been programming astro stuff heavily
in python for about 13 years now. And I have definitely been burned
before with namespace issues. So basically all of my scripts use
imports rather than "from XXX import *".
However, for quick and dirty calculator-like work in ipython, using
-pylab or some amount of "from XXX import *" is _incredibly_ useful.
And not being able to occasionally do things that way will (and does!)
keep away matlab or IDL users (I've heard that many times over the years).
We are dealing with smart people. If we teach them from day one that
namespace issues really matter (and show them why that is), that using
"import XXX" or variants is absolutely the right way to write programs
and/or scripts, and "from XXX import *" is _only_ for quick and dirty
interactive calculation, then they will do that.
When I teach students, my rule of thumb is: if you are using an editor,
use explicit imports.
We really don't want to turn people off from python by a large amount of
extra typing during their interactive, command-line tutorials. They'll
think (and they do) -- "How clunky... this is much simpler in IDL."
It really is possible to pound all of this into people's brains if you
do it from day one.
On 05/25/2011 11:03 AM, Tom Aldcroft wrote:
>> I'm going to second Joe's emphatic prohibition on "from whatever import *", for two reasons:
>> not only as a matter of purity, but also as a practical matter. Using pylab specifically, I have
>> more than once clobbered a few of my own variables when I did "from pylab import *" after
>> doing a bunch of data analysis. Or, conversely, I accidentally clobber a pylab function with
>> one of my own variables.
> We are basically in agreement and I totally discourage use of "from
> whatever import *" at the Python prompt. The people who go through the
> workshops will not even know such a construct exists! (OK, it is
> mentioned exactly once in a parenthetical side note). But clobbering
> imported functions or even built-in functions is always possible with
> Python, and that is considered a feature by many.
>> I recommend the idiom "import pylab as P". I use this with our local
>> scientists all the time, explaining to them that this sets P as an abbreviation for pylab.
>> Then we use P.plot and so on. Prepending a "P." in front of the pylab commands is really
>> very low typing overhead and visual clutter, and I haven't heard or seen any resistance to it.
>> We import numpy in a similar way.
> This is a workable option but as a style issue it's not my preference.
> I also note that "import pylab as P" violates a number of PEP8
> recommendations and I do take PEP8 pretty seriously, although
> practicality beats purity. :-)
>> (...and while we're on the subject of visual clutter, I may be the only person in the world who
>> utterly loathes iPython, but I'd like to second the request to make sure your demos work
>> in plain vanilla python as well.)
> That would require from pylab import *, or reworking them all to use
> package prefixes everywhere. I understand your position but at least
> for now will leave the examples as they are.
> **However**, based on feedback from this list we will definitely add
> substantial new content with a discussion of namespaces, what's going
> on with pylab and import *, and how to translate the interactive code
> snippets into standalone python programs.
Scott M. Ransom Address: NRAO
Phone: (434) 296-0320 520 Edgemont Rd.
email: sransom at nrao.edu Charlottesville, VA 22903 USA
GPG Fingerprint: 06A9 9553 78BE 16DB 407B FFCA 9BFA B6FF FFD3 2989
More information about the AstroPy