Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "import" implemented this way?

>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 3
>>> math.pi
3
>>> import math
>>> math.pi
3

Initial question: Why can't I get math.pi back?

I thought import would import all the defined variables and functions to the current scope. And if a variable name already exists in current scope, then it would replace it.

Yes, it does replace it:

>>> pi = 3
>>> from math import *
>>> pi
3.141592653589793

Then I thought maybe the math.pi = 3 assignment actually changed the property in the math class(or is it math module?), which the import math imported.

I was right:

>>> import math
>>> math.pi
3.141592653589793
>>> math.pi = 3
>>> from math import *
>>> pi
3

So, it seems that:
If you do import x, then it imports x as a class-like thing. And if you make changes to x.property, the change would persist in the module so that every time you import it again, it's a modified version.

Real question:

  1. Why is import implemented this way? Why not let every import math import a fresh, unmodified copy of math? Why leave the imported math open to change?
  2. Is there any workaround to get math.pi back after doing math.pi = 3 (except math.pi = 3.141592653589793, of course)?
  3. Originally I thought import math is preferred over from math import *. But this behaviour leaves me worrying someone else might be modifying my imported module if I do it this way...How should I do the import?
like image 940
octref Avatar asked Sep 18 '13 03:09

octref


People also ask

Why is it important to import?

importing foods and other goods from countries that can produce them more efficiently can actually be better for the planet than 'buying local', even if transport costs are higher. In short, free trade benefits both parties – or else it would not happen at all. This means that imports and importing are GREAT, too.

What are the reasons for import substitution?

Import substitution is a strategy under trade policy that abolishes the import of foreign products and encourages production in the domestic market. The purpose of this policy is to change the economic structure of the country by replacing foreign goods with domestic goods.

What does import * mean in Javascript?

This answer is not useful. Show activity on this post. The import * as name syntax imports all exported content of a javascript file. For example, if you want to import an entire module's contents, then access the doAllTheAmazingThings() function import * as myModule from '/modules/my-module.js'; myModule.

What does import mean in Python?

Importing refers to allowing a Python file or a Python module to access the script from another Python file or module. You can only use functions and properties your program can access. For instance, if you want to use mathematical functionalities, you must import the math package first.


1 Answers

Python only creates one copy of any given module. Importing a module repeatedly reuses the original. This is because if modules A and B imported C and D, which imported E and F, etc., C and D would get loaded twice, and E and F would get loaded 4 times, etc. With any but the most trivial of dependency graphs, you'd spend a few minutes loading redundant modules before running out of memory. Also, if A imported B and B imported A, you'd get stuck in a recursive loop and, again, run out of memory without doing anything useful.

The solution: Don't screw with the contents of other modules. If you do, that's an interpreter-wide change. There are occasionally situations where you'd want to do this, so Python lets you, but it's usually a bad idea.

like image 189
user2357112 supports Monica Avatar answered Sep 20 '22 21:09

user2357112 supports Monica