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?

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.