1

If, for example, I import as follows:

from numpy import empty, full, zeros, matrix, arange, asarray, array

Then I might have some list that I generated:

stuff = []

for i in range(N):
    stuff.append(things)

then, I realize I have to do some math! so I type:

math_stuff = array(stuff)

Since I didn't have to type numpy.array or np.array, based on how I declared my imports, how do I know that my IDE is preferring the numpy version over the built in version? Is this automatic?

I checked the docs on for numpy.array() and python's built in array(), and it looks like they both accept the same "list like" argument.

8
  • 3
    print(type(math_stuff))... But you are using numpy array, since you've imported it from numpy. If you've also imported a module/function/class/object earlier with a same name from another library. The last import will override others. Commented Mar 29, 2018 at 1:05
  • @umutto Ok, so the last import will override earlier imports and any built in functions of the same name. Got it. Commented Mar 29, 2018 at 1:06
  • 5
    @rocksNwaves: There is no built-in array name in Python. You can import the array module, but if you do that, you're explicitly creating the conflict (which is won by whoever took the name last). The code you provide doesn't show any indication that you did import array or from array import array, so the answer is there is no conflict, because there is only one thing named array in the code shown. Commented Mar 29, 2018 at 1:08
  • 1
    By the way, the idiomatic way of importing numpy is import numpy as np, rather than from numpy import stuff. That way you can write np,array, np.empty, etc., which is a pretty good balance—a lot more concise than numpy.empty, but no risk of colliding with other things named empty. Commented Mar 29, 2018 at 1:11
  • 2
    @rocksNwaves: Yar. The only names that exist without you importing them from somewhere are the built-in functions and built-in constants. Aside from those specific things, everything else comes from imports. Commented Mar 29, 2018 at 1:11

2 Answers 2

2

As the commenters have said, you can easily tell which one is being used just by looking at the most recent import statement. However, in case you get worried/confused, you can also directly check the module from which a function or class originates using Python's handy built-in reflection features.

For example, the following Python statement:

print(array.__module__)

will print out the string 'numpy.core.multiarray' if array was imported from the numpy package, or the string 'array' if it was imported from the array package.

If x.__module__ fails, explore alternatives via dir(x)

@ShadowRanger raises the good point that some Python objects don't have the __module__ property. In particular, if you run just import array, then array is a module and the print(array.__module__) call will fail. In these kinds of situations you can always discover what reflection information is actually available via the dir() function.

dir() is easily my favorite feature of Python. For any Python object x, dir(x) prints out the complete list of the attributes of x. For example, given that you just ran import array, executing dir(array) would then print out:

['ArrayType',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_array_reconstructor',
'array',
'typecodes']

Which shows that even though the array module lacks __module__, if does have other reflection information available such as __name__ and __file__.

Sign up to request clarification or add additional context in comments.

1 Comment

Although if you just did import array, rather than from array import array, print(array.__module__) will raise an AttributeError, since __module__ is not defined on modules themselves.
1

The best way is probably to keep your namespaces clean, if you can:

do: import numpy or import numpy as np,

instead of: from numpy import empty, full, zeros, matrix, arange, asarray, array

In case it is not up to you, and it is unclear what came earlier, help(array), or repr(array), or type(array) will be handy. (as mentioned in the comments)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.