I am new to Python as I want to expand skills that I learned using R. In R I tend to load a bunch of libraries, sometimes resulting in function name conflicts.
What is best practice in Python. I have seen some specific variations that I do not see a difference between
import pandas
, from pandas import *
, and from pandas import DataFrame
What are the differences between the first two and should I just import what I need. Also, what would be the worst consequences for someone making small programs to process data and compute simple statistics.
UPDATE
I found this excellent guide. It explains everything.
Importing Modules To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.
When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.
For the most part, modules are just Python scripts that are stored in your Lib or Lib/site-packages folder, or local to the script being run. That's it.
When reading other people's code (and those people use very different importing styles), I noticed the following problems with each of the styles:
import modulewithaverylongname
will clutter the code further down with the long module name (e.g. concurrent.futures
or django.contrib.auth.backends
) and decrease readability in those places.
from module import *
gives me no chance to see syntactically that, for instance, classA
and classB
come from the same module and have a lot to do with each other. It makes reading the code hard. (That names from such an import may shadow names from an earlier import is the least part of that problem.)
from module import classA, classB, functionC, constantD, functionE
overloads my short-term memory with too many names that I mentally need to assign to module
in order to coherently understand the code.
import modulewithaverylongname as mwvln
is sometimes insufficiently mnemonic to me.
Based on the above observations, I have developed the following style in my own code:
import module
is the preferred style if the module name is short as for example most of the packages in the standard library. It is also the preferred style if I need to use names from the module in only two or three places in my own module; clarity trumps brevity then ("Readability counts").
import longername as ln
is the preferred style in almost every other case. For instance, I might import django.contrib.auth.backends as djcab
. By definition of criterion 1 above, the abbreviation will be used frequently and is therefore sufficiently easy to memorize.
Only these two styles are fully pythonic as per the "Explicit is better than implicit." rule.
from module import xx
still occurs sometimes in my code. I use it in cases where even the as
format appears exaggerated, the most famous example being from datetime import datetime
(but if I need more elements, I will import datetime as dt
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With