The difference between from module import and import

巴扎黑
Release: 2016-12-09 14:42:08
Original
2090 people have browsed it

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()
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!