PythonTip: "import from" Considered Harmful
Namespace pollution is the main argument against wildcard imports:
While explicit unqualified imports are
There is, however, a harmful side-effect of unqualified imports:
The assertion fails, because the output from
For qualified imports, the attribute's value is retrieved on every use so the following example works as you would expect:
Python imports values, not names.
The interpreter makes a new variable in the importing
module with the reference of the variable from the exporting module.
When the first example assigns to
Here's the first example rewritten with
While this is not the typical form of using the
Python also creates a new variable for a module import. All of these names are all "the same", that is, they are attributes, and you can do strange things like this:
The assertion passes, because the reference to the module
BusinessBookReview: The Structure of Scientific Revolutions (3rd Edition)The Psychology of Software TestingOwn VirtuallyBookReview: Fool's Gold: How the Bold Dream of a Small Tribe at J.P. Morgan Was Corrupted by Wall Street Greed and Unleashed a CatastropheBookReview: How to Be Alone: EssaysBookReview: Eat, Pray, Love▶ More▲ Most Recent
|back to top||© 2017 Rob Nagler||Software by bivio|