When do you need to specify module?

This is the place for queries that don't fit in any of the other categories.

When do you need to specify module?

Postby ppaarrkk » Fri Jun 14, 2013 10:53 am

For example :
Code: Select all
import numpy as np
a1 = np.linspace (1, 10)

Sometimes I can write (i.e. without np.) :
Code: Select all
a1 = linspace (1, 10)

In this case, sometimes the interpreter complains that linspace is not defined, but other times it gives me an answer. Presumably, this is to do with the way a module is imported.

So, when do I need:
Code: Select all
np.linspace()

and when only:
Code: Select all
linspace()

Thankyou.
Last edited by Yoriz on Fri Jun 14, 2013 4:51 pm, edited 1 time in total.
Reason: Added code tags
ppaarrkk
 
Posts: 5
Joined: Fri May 17, 2013 11:15 am

Re: When do you need to specify module?

Postby Mekire » Fri Jun 14, 2013 12:40 pm

If you import normally:
Code: Select all
import numpy
then you have to access the function fully qualified:
Code: Select all
numpy.linspace

If you import using as:
Code: Select all
import numpy as np
then you have to access the function qualified by the name you chose:
Code: Select all
np.linspace

If you specifically import that function:
Code: Select all
from numpy import linspace
You can simply access it via:
Code: Select all
linspace
this should only be used in moderation.

DO NOT USE THE METHOD BELOW THIS LINE
If you collapse everything into your global namespace with a star import:
Code: Select all
from numpy import *
You can also access it directly by name:
Code: Select all
linspace
but you are an inherently bad person.

-Mek

Yoriz - Rather then hijack the OP thread with a discussion about showing bad ways of doing things i've deleted my post and the following replys to it.
User avatar
Mekire
 
Posts: 982
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests