Is it unpythonic to use a package imported by another package, or should I import it directly?

For example, I want to use the numpy isnan function. I already downloaded the pandas library:

 import pandas as pd pd.np.isnan(1) #=> False 

It works, but does it have flaws? Or should I write

 import pandas as pd import numpy as np np.isnan(1) #=> False 

What is good practice?

+6
source share
1 answer

You should use the second approach for at least four reasons:

  • As @abarnert notes in the comments, it follows the official guidelines for Python code, as stated in PEP 0008 under the public and internal interfaces . In particular, PEP says:

    All undocumented interfaces should be considered internal.

    and

    Imported names should always be considered implementation details. Other modules should not rely on indirect access to such imported names unless they are an explicitly documented part of an API-containing module, such as os.path or the __init__ module of a package that provides functionality from submodules.

    Since NumPy is an undocumented aspect of the Pandas library (it is not mentioned either in help(pd) or on the official website), it should not be considered as an official part of Pandas.

  • โ€œExplicit is better than implicit,โ€ and the second approach makes it explicit that we use the NumPy library directly in the code. The first approach, however, โ€œslipsโ€ in the Pandas library.

  • Code analysis tools will not be able to see that your code uses NumPy directly. This can generate false data about your code (for example, what dependencies it has).

  • The fact that Pandas contains NumPy is nothing more than an implementation detail. Meaning, if the creators of Pandas ever changed their internal code to somehow change this detail, all of your Numpy code might suddenly break if it really isn't. Numpy and Pandas are two separate things and should be treated as such.

+10
source

Source: https://habr.com/ru/post/979534/


All Articles