简体   繁体   中英

Importing numpy statement in script

I am a matlab coder and have dabbled in and out of Python for various things. One very basic concept/thing in python which I have not got to the bottom of relates to the from and import statements.

When i import bitarray from the bitarray module (am I correct saying this?) using example 1) I just call it's methods when I want to use them in the script that follows. However, when i import numpy I cannot use the code shown in 2) as it throws an error, i have to use the statement in 3) then type np. before using any of its methods, which makes sense. I can also import bitarray using similer syntax which i have shown in 4) below....... why does 2) not work when 1) does? I am praying for a simple explanation :) .....

1)

from bitarray import bitarray

2)

from numpy import numpy

3)

import numpy as np

4)

import bitarray as pp

This is due to the different package structures between bitarray and numpy ; specifically, one contains an object with the same name as the module itself, the other doesn't.

The bitarray module contains a class called bitarray , (similarly, for the example, the datetime module in the standard library contains the datetime class) . Hence the following are equivalent:

from bitarray import bitarray
ba = bitarray(...)

and

import bitarray
ba = bitarray.bitarray(...)

In the former we import the class bitarray directly into the current namespace from the module; in the latter we import the whole module as bitarray and need to access the class by specifying the module namespace.

By contrast, numpy does not contain an object named numpy , so

from numpy import numpy 

doesn't work.


The as is separate; that simply allows you to provide an alias for whatever you're importing, so any of the following will work fine:

import numpy as np
a = np.array(...)

from bitarray import bitarray as pp
b = pp(...)

import bitarray as pp
b = pp.bitarray(...)

As mauve comments above, you can use the * "wildcard import" to import everything, so:

from numpy import *
from bitarray import *

will import every top-level name in both modules into the current namespace. Afterwards, eg

a = array(...)
b = bitarray(...)

will both work fine. However, this is not recommended as it is generally a bad idea - for example, numpy contains a definition of all which will shadow the built-in function of the same name if you import everything.

Also, it makes it much more difficult for anyone reading your code (including a later version of you!) to figure out where the classes and functions being used have come from. Compare:

from numpy import *
from random import *

a = choice([1, 2, 3]) # which module did choice come from?
b = all(x == y for x in l) # is this numpy's all or the built-in?

with the much clearer:

import numpy as np
import random

a = random.choice([1, 2, 3]) # clearly from random
b = all(x == y for x in l) # must be the built-in, not np.all()

1) If bitarray is a function or class name in the library bitarray then it is perfectly valid.

2) This doesn't make sense because numpy doesn't have a function named numpy .

3) This is fine because you are importing a library and assigning it an alias.

4) This is fine as well because again you are simply importing under an alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM