Home  >  Article  >  Backend Development  >  The difference between from module import and import

The difference between from module import and import

巴扎黑
巴扎黑Original
2016-12-09 14:42:082131browse

I am using codecademy to learn python recently, and I encountered some mistakes in the questions. I will record them briefly

For example, from math import sqrt imports sqrt as the method of this file. When using it, you only need to call sqrt directly.

And if the import is import math, then the method of math.sqrt must be used when calling.

So what if a function with the same name is defined in your own file? Let’s try it out

Python code

from math import sqrt  
def sqrt(n):  
    return n  
print sqrt(13689)  
raw_input()

Run the py file and output 13689, so the function you defined will be automatically blocked.

So it is a better way to quote import math.

In addition, python does not support import math.sqrt like java


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn