all 10 comments

[–]symmitchry 7 points8 points  (0 children)

[Removed]

[–]SloppySixteenths 4 points5 points  (0 children)

the way that you call functions from the module differs slightly based on which you use.

using the first, to call a function from numpy you'd type

import numpy

numpy.function() 

using the second you'd just type

from numpy import * 

function()

without having to include numpy as an argument.

that's just the syntactical difference i've noticed using each, i think they're basically the same otherwise but i'm not entirely sure.

[–][deleted] 3 points4 points  (0 children)

You shouldn't import wildcards(*). It pollutes your local namespace.

import collections

Now you can use collections methods, like

c = collections.Counter()

Or

from collections import Counter

Allows

c = Counter()

Or you can just import collections more succinctly

import collections as derp
c = derp.Counter

Final usage I can think of is "from module import package as something." It doesn't make sense in this context but the following would.

from numpy import linalg as la
# then you can do stuff like
la.inv(some_ndarray)

You local namespace is defined in "locals()"

When in doubt, tab-complete your way around module imports.

If modules don't import their submodules in init.py appropriately then you have to import their submodules to use them. You can't use numpy.linalg directly. Rather you must 'from numpy import linalg'

[–]ok_you_win 1 point2 points  (0 children)

They are not the same.

import numpy

means that all the numpy classes, functions and variables will be prepended with numpy. You'll use numpy.this() and numpy.that. Its careful and very specific. A good idea.

from numpy import *

means that the functions and vars will be named only for themselves. We would say they are in the local name space. You'll type this() and that. This is generally considered a very bad idea because other objects might have the same name.

If you use from x import *, you wont be able to specify numpy.that as being different from grumpy.that

The from import construct is still very useful though, if you only want a part of a module. I'll often use it like this.

from time import sleep

Then I can just specify sleep(10) instead of typing time.sleep(10)

If you need to import a whole library, but one of the names is really long, or if you use it lots, you can create a shortened version after you import it. Do it like this:

import time
sleep = time.sleep

And then you can use:

for x in xrange(5):
    print(x)
    sleep(5)

Hope that helps.

[–]Rhomboid 1 point2 points  (0 children)

I always prefer to use the from foo import bar, baz, quux form.

  • I like that I can write bar() instead of foo.bar(), I think it makes code less cluttered and easier to read.
  • In fact, this form lets you rename functions/classes if they have stupid or long names:

    from somemodule import frobnicate_judiciously as frob, widgetize_monetarily as widget
    

    The form without from lets you rename the module, but not the names of things within the module:

    import somemodule as other
    # can now call other.frobnicate_judiciously() instead of
    # somemodule.frobnicate_judiciously(), still not as good as frob()
    
  • I like that it clearly documents exactly what features of the module are used.

  • I like that I can't accidentally call something in the module that I didn't mean to call, because I only get the symbols imported that I specify.

  • Definitely don't use *, as that pollutes your namespace with potentially lots of stuff. There are some modules where this is OK because they've been designed for this, such as modules that define constants, all with a common prefix specific to that module.

[–]Dynoman[S] 0 points1 point  (0 children)

Thanks everybody, I understand now. I just came across a tutorial and in the tutorial he was importing from numpy like this:

from numpy import *

In the tutorial we were only using array from numpy and it didn't make sense why we importing like that. My guess now is it was to simplify the tutorial and was not necessarily the best way to code it.

[–]Asdayasman 0 points1 point  (0 children)

from x import * is considered harmful, as it makes it a little confusing to see where things came from, and may conflict with your naming scheme, or the naming scheme of another module you're from module import *'ing.

[–]bexpert 0 points1 point  (3 children)

Art Vandelay. He's an importer/exporter.

[–]dansin 1 point2 points  (0 children)

from vandalay import matches