"import *" vs "import abc, ..." in the notebook

(Xu Fei) #1

I know “import *” is not recommended in Python as it pollutes the global namespace, but I can’t seem to find a thread or anything in the video talking about the advantage of doing it here in the notebooks (other than saving some initial typing time). Wouldn’t it be more beneficial to import fastai modules explicitly instead of what’s been done in the notebooks as “import *”?

(Ramesh Sampath) #2

Jeremy had talked about this briefly in one of the early lectures. He distinguishes them as - Software Engineering vs. Working with Data (I don’t remember the exact quotes - happy to be corrected). Basically - He feels more comfortable importing * so he has access to ALL the functions and Classes while working on the notebook. He may not know what he will end up using and fits his workflow.

But if you would rather only import specific Classes and methods, then by all means, do them in your own work and notebooks. I follow this in my own notebooks when I use any library including fast.ai. It also forces me to dig into the library and see where each function / classes are defined and get better intuition.

(Xu Fei) #3

Wow thanks for the quick and thorough answer Ramesh :slight_smile: I find myself doing from fastai.xyz import abc to learn the package better, but now it all makes sense!

(Jeremy Howard) #4

import * works better when just a few classes are exported, rather than lots of functions. One thing that I think would be useful to add to fastai is to use __all__ to only export by default the things that are designed for public consumption (i.e. stuff that’s used by other modules or notebooks).

(Xu Fei) #5

Thanks for the reply @jeremy I’m just learning __all__ and found this thread quite helpful. I’d be happy to submit PR but it looks as though we need to know the whole library inside out to make it work…