Learn
Functions
Here Be Dragons

Universal imports may look great on the surface, but they’re not a good idea for one very important reason: they fill your program with a ton of variable and function names without the safety of those names still being associated with the module(s) they came from.

If you have a function of your very own named sqrt and you import math, your function is safe: there is your sqrt and there is math.sqrt. If you do from math import *, however, you have a problem: namely, two different functions with the exact same name.

Even if your own definitions don’t directly conflict with names from imported modules, if you import * from several modules at once, you won’t be able to figure out which variable or function came from where.

For these reasons, it’s best to stick with either import module and type module.name or just import specific variables and functions from various modules as needed.

Instructions

1.

The code in the editor will show you everything available in the math module.

Click Run to check it out (you’ll see sqrt, along with some other useful things like pi, factorial, and trigonometric functions.

Folder Icon

Take this course for free

Already have an account?