I just started learning Python. I think there should be a difference between import and fromimport, but I did not answer my questions after I searched the internet. Ps: I am using python3.5. For example, datetime: fromdatetimeimportdatetimeprint (datetime. now () I just started learning Python. I think there should be a difference between import and from import, but I did not answer my questions after searching the Internet.
Ps: I am using python3.5.
For example, datetime:
from datetime import datetimeprint(datetime.now())
Reply: from import: Take the mineral water out of the car and give it to me.
Import: Give me the car.
The subject may be confused. Why do people ask themselves to take out the "car in the car" because they are two different cars, the one in it is a toy car. I think it is easy to make a mistake because the datetime package is used.
Datetime contains a sub-item of datetime with the same name. now is available under this sub-item.
Because there is a sub-item with duplicate names, beginners may be confused.
Change the landlord's questions into a package to answer the landlord's questions. It should be clear:
The following three statements are correct. The join function of the path sub-item in the OS package is called to add the paths into a complete path:
from os.path import joinprint(join('C:\\windows', 'system32'))
There are differences between the two import Semantics
import datetimeprint(datetime.datetime.now())
Although you import datetime, now is the now method of the datetime class in the datetime module, so you need to write it as datetime. datetime. now ......
From datetime import datetime is because the datetime class is introduced from datetime. Instead of this module, this class is introduced. Naturally, it is datetime. now.
from module import Something
This is purely a question about the naming design of the datetime library. If you look at the name space logic, you will understand how to use import a as much as possible. When accessing B, you can use. b.
Use from a import B in a controlled manner, so that B can be used directly.
Avoid using from a import *. This method will pollute the namespace and cannot intuitively display the imported objects.
ToolboxThere are
Scissors, Yes
Pliers, Yes
ScrewdriversAnd so on.
Now you want to screw.
Then you can perform the following operations:
ToolboxRenale
ScrewdriversAnd then use
ScrewdriversGo
Screw (from import ).
Another operation is: Take
ToolboxIf not, go directly.
Import). The import xx module requires "module. function" for each function in the module.
From xx import fun directly imports a function in the module, and fun () is available directly.
Tell you the big way: from xx import * all functions in this module can be used directly.