Background:
The Python 3 development team implemented changes to the import statement in Python 2, aimed at improving its clarity and reducing ambiguity.
Relative Imports:
What is a relative import?
Example:
In the example directory structure, derived.py would previously import BaseThing from base.py using the statement:
from base import BaseThing
Python 3 requires explicit relative imports:
from .base import BaseThing
Star Imports:
Restrictions in Python 3:
Use Case in Python 2:
Example:
In Python 2, the following code was valid:
def sin_degrees(x): from math import * return sin(degrees(x))
Python 3 requires a more explicit approach:
def sin_degrees(x): from math import sin, degrees return sin(degrees(x))
The above is the detailed content of How Have Python 3's Import Statement Enhancements Improved Code Clarity and Reduced Ambiguity?. For more information, please follow other related articles on the PHP Chinese website!