In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the difference between import and from import in Python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it!
The system comes with module (library) ````cppimport retarget = 'abc1234xyz're.search (' (\ d+)', target) but sometimes you may see someone write code like this: ```pythonfrom re import searchtarget = 'abc1234xyz'search (' (\ d+)', target)
So what's the difference between the two import methods?
Let's use the type function to see their types:
> import re > type (re) > from re import search > type (search)
As you can see, re imported directly using import re is a module class, that is, a module. We call it a regular expression module. When we from re import search, this search is a function class, and we call it the search function.
A module can contain multiple functions.
If you have decided to use only the search function in your code and will not use any other functions in the regular expression, then you can use either method, which makes no difference.
However, if you want to use multiple functions under regular expressions, or some constants, then using the first solution will be more concise and clear.
For example:
Import rere.search ('c (. *?) xboxes, flags=re.S) re.sub ('[a-zA-Z0-9]','* *', target, flags=re.I)
In this example, you use re.search,re.sub,re.S and re.I, respectively. The latter two are constants and are used to ignore newline characters and case.
However, if you use from re import search, sub, S, I to write the code, the code looks like this:
Import research ('c (. *?) xboxes, flags=S) sub ('[a-zA-Z0-9]','* *', target, flags=I)
It looks concise, but once you have more lines of code, it's easy to forget what the S and I variables are. And the functions we define ourselves are likely to be named sub or search, thus overriding the two functions of the same name under the regular expression module. This can lead to a lot of potential bug that are imperceptible.
Let me give you another example. Python datetime module, we can directly import datetime, at this time we import a datetime module
Output is: class'module'
But if you write from datetime import datetime, then the datetime you import is a type class:
Output is: class'type'
Because datetime is imported in this way, it is a type of Python that represents data that contains date and time.
Although the names of the datetime imported by these two import methods are the same, their meanings are completely different. Please observe the following two ways of writing:
Import datetimenow = datetime.datetime.now () one_hour_ago = now-datetime.timedelta (hours=1) from datetime import datetime, timedeltanow = datetime.now () one_hour_ago = now-timedelta (hours=1)
The second way of writing seems simple, but it is actually more troublesome to change. For example, I also need to add a variable today to record today's date.
For the first piece of code, we only need to add one line:
Today = datetime.date.today ()
But for the second line, we need to first modify the code in the import section:
From datetime import datetime, timedelta, date
Then you can change the code: today = date.today ()
In this way, you have to modify two places, which adds to the burden.
Third-party library
In the code that uses some third-party libraries, we will see something like this:
From lxml.html import fromstring
Selector = fromstring (HTML)
But we can also write as follows:
From lxml import html
Selector = html.fromstring (HTML)
However, the following writing can lead to an error:
Import lxml
Selector = lxml.html.fromstring (HTML)
So what is the lxml.html here?
This situation is common in particularly large third-party libraries, which can handle many types of data. For example, lxml can process both xml data and html data, so this kind of library will be divided into molecular modules, and the lxml.html module is responsible for html-related data.
Give it a try.
In the code that uses some third-party libraries, we will see something like this:
From lxml.html import fromstring selector = fromstring (HTML)
But we can also write as follows:
From lxml import htmlselector = html.fromstring (HTML)
However, the following writing can lead to an error:
Import lxmlselector = lxml.html.fromstring (HTML)
So what is the lxml.html here?
This situation is common in particularly large third-party libraries, which can handle many types of data. For example, lxml can process both xml data and html data, so this kind of library will be divided into molecular modules, and the lxml.html module is responsible for html-related data.
Implement a variety of import methods by yourself
We will now write our own code to implement these various import methods.
Let's create a folder DocParser, in which we create two files, main.py and util.py, with the following contents:
Util.py file:
Def write (): print ('write function is called!') Main.py file: import utilutil.write ()
Now let's change the import method of main.py (the result is the same as above):
From util import writewrite ()
Now, let's create a folder microsoft and add a file called parse.py:
From util import writewrite ()
At this point, we call it in main.py:
Def read (): print ('I am the read function in parse.py under the microsoft folder')
We can also use another way:
From microsoft import parseparse.read ()
However, you cannot import microsoft directly
Import microsoft
Microsoft.parse.read
Whether you are using import xxx or from xxx.yyy.zzz.www import qqq, what you import is either a module (corresponding to the file name of a .py file), or a function name, class name, or variable name in a .py file.
Whether it is import xxx or from xxx import yyy, you cannot import the name of a folder.
There may be a situation where the name of a function is the same as the name of the file, for example:
In the microsoft folder, there is a microsoft.py file with a function called microsoft, so your code can be written as follows:
From microsoft import Microsoft`
Microsoft.microsoft ()
But please note that you are still importing the module here, but the name of the microsoft.py file is exactly the same as the name of the folder in which it is located.
These are all the contents of this article entitled "what's the difference between import and from import in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.