Python “from matplotlib import pyplot as plt” == “import matplotlib.pyplot as plt”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30558087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:38:57  来源:igfitidea点击:

Is "from matplotlib import pyplot as plt" == "import matplotlib.pyplot as plt"?

pythonmatplotlibpython-import

提问by megashigger

from matplotlib import pyplot as plt

import matplotlib.pyplot as plt 

Are the above statements equivalent? Which is more readable/better form?

以上陈述是否等价?哪个更易读/更好的形式?

采纳答案by Eric Appelt

Even though they are equivalent, I think there is a pretty good argument that the second form import matplotlib.pyplot as pltis objectively more readable:

尽管它们是等价的,但我认为有一个很好的论点,即第二种形式import matplotlib.pyplot as plt客观上更具可读性:

  1. It is generally customary to use import matplotlib.pyplot as pltand suggested in the matplotlib documentation (see http://matplotlib.org/users/pyplot_tutorial.htmletc...) so this will be more familiar to most readers.

  2. import matplotlib.pyplot as pltis shorter but no less clear.

  3. import matplotlib.pyplot as pltgives an unfamiliar reader a hint that pyplotis a module, rather than a function which could be incorrectly assumed from the first form.

  1. 通常习惯于import matplotlib.pyplot as plt在 matplotlib 文档中使用和建议(参见http://matplotlib.org/users/pyplot_tutorial.html等...),因此大多数读者会更熟悉这一点。

  2. import matplotlib.pyplot as plt更短但同样清晰。

  3. import matplotlib.pyplot as plt给不熟悉的读者一个提示,即pyplot是一个模块,而不是一个可能从第一种形式错误地假设的函数。

回答by Padraic Cunningham

They both work the same so it is up to you which you prefer, personally I don't like typing so I would prefer the second.

它们的工作方式相同,因此您喜欢哪种取决于您,我个人不喜欢打字,所以我更喜欢第二种。

from matplotlib import pyplot as plt

import matplotlib.pyplot as plt1

print(dir(plt) == dir(plt1))
True

回答by Priya Bharti

Yes,Both are same. It's Depends upon you what you prefer to import.

是的,两者都是一样的。这取决于你喜欢导入什么。

Personally I Like to Write :

我个人喜欢写:

from matplotlib import pyplot as plt

Because it's look more clear and clean to me.

因为它对我来说看起来更清晰和干净。