r/Python • • Jun 09 '16

Scientific Python Cheat Sheet

https://ipgp.github.io/scientific_python_cheat_sheet/
476 Upvotes

62 comments sorted by

View all comments

6

u/hharison Jun 10 '16

This is kind of silly, and there's no reason to prefer one way or the other, but does anyone else do from matplotlib import pyplot as plt rather than import matplotlib.pyplot as plt?

0

u/[deleted] Jun 10 '16

[deleted]

1

u/XtremeGoose f'I only use Py {sys.version[:3]}' Jul 28 '16

You know ipython has a %matplotlib inline now? %pylab is depreciated.

1

u/[deleted] Jul 28 '16

Yeah, but all my muscle memory is for pylab and not matplotlib or plt.

2

u/deaddodo Jun 10 '16

The more "pythonic" way of handling imports is argued to be:

import blah[.blahbity] [as blah2]

but both are perfectly acceptable.

I generally avoid "from...." since it makes the import lines look cluttered (IMO).

1

u/hharison Jun 11 '16

Just curious if you weren't using an alias but wanted to do a nested import without the parent namespace would you do import scipy.stats as stats or from scipy import stats? Just thinking out loud here...

1

u/PeridexisErrant Jun 11 '16

his is kind of silly, and there's no reason to prefer one way or the other, but does anyone else do from matplotlib import pyplot as plt rather than import matplotlib.pyplot as plt?

I prefer the latter; most concise is not always most elegant but there is a strong correlation.

There actually is a (potential, IIRC...) difference in behaviour when you're monkeypatching something. Basically from x import y detaches y from the parent namespace and changes don't propagate back, while changing x.y after import x.y will change the instance used in other modules. This should only come up when you have circular patched imports, I think (I'm not proud of that code).

1

u/hharison Jun 11 '16

Good point. Come to think of it, I think the reason I do it with from is similar structure to other nested imports. If I wasn't using an alias I would do from matplotlib import pyplot rather than import matplotlib.pyplot as pyplot. So it makes sense to do it the same way with an alias. If I want to switch between using an alias and not using an alias, I don't also switch the structure of my import statement.

I just realized this is probably my motivation. I don't think it's a good reason, readability is more important, so I'll probably switch to the traditional way.

1

u/TheBlackCat13 Jun 11 '16

I find it harder to mentally parse your version. It is longer and has an additional unneeded statement that makes it more complex.