Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confirming the difference between import * and from xxx import *

I was surprised to find out that

import foo

and

from foo import *

had different effects on global members. I wanted to confirm that my experiments are the correct behavior.

In the first example, changing a member in module foo will reflect in all code that imports foo. However, changing that member in the later case only seems to affect the file into which it was imported. In other words, using the later approach will give each importing file its own copy of the members from foo.

the behavior i want is to have access to foo.x from all files, be able to change it from all files, and have that change reflected in all files ( a true global if you will).

like image 265
D.C. Avatar asked Dec 14 '10 06:12

D.C.


People also ask

What is the difference between import library and from library import *?

import imports the whole library. from import imports a specific member or members of the library.

What does it mean import * in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.

What is the difference between import and from import in Python?

Explain the difference between import and from import statement, with example. It import entire module and so everything inside the module will be imported like functions, constant, variables. from <module> import statement imports selected items , but to use these items we don't have to prefix module name.

What is import and from import statement?

The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement. This lets you import only the exact functions you are going to be using in your code.


1 Answers

Yes, your observations are correct. This is a consequence of the way that binding works in Python.

When one does

import foo

then foo becomes a global name that references the module foo. When one does

foo.bar = 7

Then the reference is followed and the object foo is loaded. Then 7 is stored in the bar attribute.

When another module imports foo, it just pulls the object out of sys.modules['foo'] and gets the modified value.

When one does

from foo import bar

globals()['bar'] is set to reference foo.bar. When one later does

 bar = 7

globals()['bar'] no longer references foo.bar but references a copy of 7. That is, the original binding in the global scope of the importing module is simply replaced.

In the fist example, one is modifying attributes of an object that is stored in sys.modules and will be common to all modules that have imported it. In the second example, one is modifying the global scope of the importing module.

If one was to do something along the lines of

 from foo import fobaz
 fobaz.foobar = 7

Then that change would be propagated to other importing modules because one is not overwriting the global reference but following it to modify an attribute of the object that it points to. So essentially, you should be able to modify mutable objects so long as you don't overwrite the global binding.

I think that something like this is the closest that you are going to be able to cleanly get to a true global in python. As a language, it greatly values namespaces.

like image 161
aaronasterling Avatar answered Nov 25 '22 12:11

aaronasterling