Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Summarizing Import Statements

00:00 Well, before showing you another slide, let me show you the slide from before. I was saying that there are four variations of the import statement, and so far you’ve seen three, but there is a fourth one: from <module> import <name> as <other_name>.

00:14 So this one is basically a combination of two and three. So you won’t see a demo for this one, but you can try it out yourself, and if something doesn’t work, just write in comments below. And now I will summarize the first three for you.

00:29 Again, the fourth variation isn’t in the summary because it’s a combination of the import statements you are seeing in this slide. And this list summarizes what you’ve learned about importing modules so far.

00:41 With import <module>, you import the entire namespace of <module> into the name <module>. Imported module names can be accessed from the calling <module> with <module>.<name>.

00:53 In your code, that was import adder, and then you call the add() function with adder.add().

01:00 When you use import <module> as <other_name>, you import the entire namespace of <module> into the name <other_name>.

01:08 For example, when you wrote import adder as a, then you were able to access the add() function with a.add().

01:17 With from <module> import <name1>, <name2>, you import only the names <name1>, <name2>, and so on from the module.

01:26 The names are added to the calling module’s namespace and can be accessed directly. In your case, that was from adder import add, double, and then you were able to access both functions directly by calling their names without any prefixes. In general, import <module> is the preferred approach because it keeps the imported module’s namespace completely separate from the calling module’s namespace. Moreover, every name from the imported module is accessed from the calling module with the <module>.<name> format, which immediately tells you in which module the name originates.

02:05 There are two reasons why you might use the import <module> as <other_name> format. One is if the module name is long and you wish to import an abbreviated version of it.

02:16 And the second reason is the module name clashes with an existing name in the calling module. The statement import <module> as <other_name> still keeps the imported module’s namespace separate from the calling module’s namespace.

02:30 The trade-off is that the name you give the module might not be easily recognizable as the original module name.

02:38 Importing specific names from a module is generally the least preferred way to import code from a module. The imported names are added directly to the calling module’s namespace, completely removing them from the context of the calling module.

02:53 The various import statements allow you to minimize the time you spend typing unnecessarily long dotted module names. That said, misusing the various import statements can lead to a loss of context, resulting in code that is more difficult to understand.

03:09 So it’s important to remember that you can and sometimes should curate your namespace when importing modules.

Become a Member to join the conversation.