Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

coding style - Importing modules in Python - best practice

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Disadvantage of each form

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.

A suitable compromise

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...