Importing modules in Python - best practice

后端 未结 6 564
时光取名叫无心
时光取名叫无心 2020-12-02 11:46

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

6条回答
  •  余生分开走
    2020-12-02 12:24

    import pandas imports the pandas module under the pandas namespace, so you would need to call objects within pandas using pandas.foo.

    from pandas import * imports all objects from the pandas module into your current namespace, so you would call objects within pandas using only foo. Keep in mind this could have unexepcted consequences if there are any naming conflicts between your current namespace and the pandas namespace.

    from pandas import DataFrame is the same as above, but only imports DataFrame (instead of everything) into your current namespace.

    In my opinion the first is generally best practice, as it keeps the different modules nicely compartmentalized in your code.

提交回复
热议问题