[AstroPy] Practical Python for Astronomers web tutorial

Vicki Laidler laidler at stsci.edu
Wed May 25 01:35:29 EDT 2011

Hi Tom,

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.

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.

(...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.)

I do agree that pylab is very valuable for this kind of tutorial and for astronomers' Python use
in general. In real life I do "from pylab import *" for quick&dirty interactive work, but I would
never teach with it and I don't do it in a session where I'm doing any non-trivial amount of 
data manipulation because I've been burned.

Vicki Laidler

From: astropy-bounces at scipy.org [astropy-bounces at scipy.org] on behalf of Tom Aldcroft [aldcroft at head.cfa.harvard.edu]
Sent: Wednesday, May 25, 2011 12:55 AM
To: jh at physics.ucf.edu
Cc: astropy at scipy.org
Subject: Re: [AstroPy] Practical Python for Astronomers web tutorial

Hi Joe,

Thanks for the detailed review and comments on the tutorial site, this
is very helpful!  In general I agree with your points and we'll plan
to tweak the content accordingly.

> 1. Don't use:
> from whatever import *
> or any shortcuts like 'ipython -pylab' that do that.  It's poor style
> and against the practice of the numpy community in all its docs and
> demos.  Remember that the numpy, scipy, and matplotlib developers can
> in the future put anything they want in their namespaces, and a new
> release could break your working code because of a namespace conflict
> that doesn't exist when you write it.  While that may not bite you for
> your workshops, it will eventually bite anyone who writes enough code
> like that.  Those problems can be hard to find.

Now I'm going risk being branded as a heretic or lazy or a bad
influence, or all of the above...

The decision to use "pylab" was a deliberate one based on the desire
to present Python as a simple and easy tool for the research
astronomers that participated in the workshops.  For a first look at
Python for analysis there is a real advantage to reducing the visual
and conceptual noise in the form of package prefix names everywhere.
For astronomers who have been using IDL or Matlab for 20 years this
would make Python seem far less attractive.  In our workshops the
majority of people were already fluent in some analysis environment
but few had much experience with Python.

On the topic of rigorously avoiding the implicit "import *", the
workshops tend to follow the Zen of "practicality beats purity".  Most
astronomers I know write only simple programs and are highly focused
on getting an answer quickly so they can have results for their talk
next week.  In that regime I think a "pylab" approach is not only
acceptable but the most efficient.  Of course there is no question
that for a package module or longer program that will get re-used
later, one needs to worry about namespaces and following best
practices.  And this would be a perfect topic for a new workshop
chapter "Beyond one-off scripts: writing real programs in Python"
Volunteers? :-).  You're right that interactive analysis is not the
whole story.

I should note that I never use "import *" in any scripts and do not
endorse ever using that construct explicitly in a script.  For
interactive plotting and analysis I use "ipython -pylab" and typing
plus execfile(), otherwise always explicit import statements.

> Also, the title of each workshop has the first word on the end of a
> breadcrumb trail and the rest on a second line.  Maybe put in a line
> break before the title.

I don't understand what this means.  Is the HTML rendering differently
for you or ??

Thanks, Tom
AstroPy mailing list
AstroPy at scipy.org

More information about the AstroPy mailing list