r/learnpython • • Jul 12 '20

Jupyter Notebooks!

Jupyter Notebooks are the tool that turned the tables for me learning python. I'm just here to evangelize them. You can run your code line by line and debug as you go in a comfortable web browser interface. It's an excellent tool, and it made it so much more efficient to learn. Check it out.

425 Upvotes

127 comments sorted by

View all comments

57

u/[deleted] Jul 12 '20

It’s great for experimenting. But beware of the way it’ll influence the way you code, especially if you’re a beginner. I’ve seen people overuse it to the point that they fail to write efficient code because their code just works well in their notebook. However, none of it becomes reproducible outside that notebook. In order to make it useful beyond that notebook, you do need to at least convert the code into more useful format using classes, functions, etc .

5

u/synthphreak Jul 12 '20 edited Jul 12 '20

Can you provide any concrete examples of someone’s coding style being influenced by the affordances of the notebook environment in this way?

9

u/[deleted] Jul 12 '20

Yes. I once had a coworker who wrote mostly in notebooks. When it came to making production code, they struggled to get everything put together. They ran into a problem where they didn’t know how to encapsulate half their code in something reproducible because the notebook had become very messy and hard for them to even understand what was going on. I’m sure if they were given more time to figure it out, they would have. But we didn’t always have that time, so we had to sit down together and write it from scratch- this time in a script as a class from the start. They were so not used to the process that I ultimately had to do it myself. They still have similar habits now, but they’re trying to write more in scripts. It just causes ineffeciency for everyone if one team member is inefficient.

I use notebooks myself, but I actually write my classes and functions in notebook cells first and then immediately paste those into scripts in an appropriate manner. I then import those classes and functions instead of having them in my notebook. This is to ensure my notebook doesn’t become one giant hot mess.

4

u/synthphreak Jul 12 '20

I imagine that at a minimum, periodically restarting the kernel and running all cells will go some way toward fixing this problem.

As a notebook grows in size, complexity, and age, it becomes easy to lose track of what’s defined and where, what isn’t defined, and in what order cells need to run to work. But periodically rerunning everything and making sure it still runs will help keep your organization in check, forcing you to write your notebooks in a more “script-like” manner.

But TBH I really don’t use notebooks that much. So I’m curious what others think about this.

2

u/g0lem Jul 12 '20

You've pretty much hit the nail on the head. The problems you mention are quite hard or at least frustrating for a beginner to debug. Notebooks can be used correctly if you often refactor (groups of) cells in functions or simply delete them (if not useful for the end goal), but on the other hand, there are good debuggers in Pycharm and other IDEs. I don't think there are definitive arguments for and against one particular tool/environment for development. However, notebooks shouldn't grow too much in size/complexity/age, because at that point they need to be refactored/packaged in modules.