-1

While reading some python code, I encountered 2 ways of importing user-defined modules.

The first one;

import config

The second one;

from config import *

What is the difference between the two? What happens if both lines of code are present? Any side effect?

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • 6
    The second way will pollute your current namespace with almost everything that is in config. – Marcin Dec 11 '14 at 02:35
  • @Marcin: When I want to use variable declared in config module like this `config.variable`, `from config import *` cannot work. Does that mean one has to use `import config` in this situation, even if it is a big pollutant? Are there better ways to avoid pollution? – guagay_wk Dec 11 '14 at 02:42
  • 1
    There is some pretty decent documentation with examples about this available at [python.org](https://docs.python.org/2/tutorial/modules.html#importing-from-a-package). The down vote isn't mine, but because this information is widely available and you don't mention looking anywhere else for it, you may attract down votes. – Minnow Dec 11 '14 at 02:45
  • `from config import *` might not import everything. By default, private variables (i.e. those starting with single underscore) are not being imported. – Marcin Dec 11 '14 at 02:46
  • possible duplicate of [What are good rules of thumb for Python imports?](http://stackoverflow.com/questions/193919/what-are-good-rules-of-thumb-for-python-imports) –  Dec 11 '14 at 02:47
  • `-1` for telling people not to downvote. If you are "in danger of being blocked" perhaps you should put more effort into researching your questions *before* asking. –  Dec 11 '14 at 02:47
  • @Lego Stormtroopr: Ok. I will remove that part. I am not exactly telling people not to downvote. I am just telling them to at least tell me why they are downvoting if they want to downvote which is what you have done. THanks, anyway. – guagay_wk Dec 11 '14 at 02:50
  • Just been banned from Stack Overflow. Sigh ... – guagay_wk Dec 11 '14 at 02:55

2 Answers2

4

Say there's a module named unicorn, with global contents stack, over, and flow.

  • If you do import unicorn, these can be accessed as unicorn.stack, unicorn.over, and unicorn.flow.
  • If you do import unicorn as U, these can be accessed as U.stack, U.over, and U.flow.
  • If you do from unicorn import stack, the stack object from unicorn is copied into the global variable stack.
  • If you do from unicorn import *, stack and other all global objects in the module (except private ones whose names begin with an underscore) are copied into your global namespace. This can be fine, but later updates to the unicorn module might add things that cause your program to behave unexpectedly. For example, if you have variables called user and server, a future update to unicorn that adds super, user, server, and fault may cause it to misbehave.
  • If you do from unicorn import stack, over, flow, only those things you specify get copied in, and your program keeps working even after more things get added to unicorn.
  • If you do from unicorn import stack as H, you get stack from the unicorn module, but in your program's global namespace it's called H. This can help keep your lines under 80 characters if you're using a particular function or variable really often.
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
-1

The code

import config

just import the module named config,if you want to use a method of it,you need to use something like myname = config.getName() .

The code

from config import *

will import everything exported by config to current namespace. Now if you want to use the getName method in config,the code is something like myname = getName()

We always use the first importing way,because you can use different methods with the same name exported by more than one module. For example,we can use two getName method below:

import config
import mydata

print config.getName()#print Jim
print mydata.getName()#print Tom

When you do it in the second way like,the last import code will import the getName method replace the one you has imported by the first import code:

from config import *
from mydata import *

print getName()#print Tom

So you can see,the current namespace is polluted by the second way.

tianwei
  • 1,859
  • 1
  • 15
  • 24
  • This is wrong, the latter won't work as `config` and `mydata` aren't defined in that space. They won't print anything and `print config.getName()` will throw an exception. –  Dec 11 '14 at 02:56
  • @LegoStormtroopr so sorry that I just copy the code,and do not modify it.:( – tianwei Dec 11 '14 at 03:20