简体   繁体   English

我怎么知道我是在调用numpy.array()还是使用内置数组函数?

[英]How do I know if I am calling numpy.array() or using built in array function?

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? 由于我不必输入numpy.array或np.array,根据我如何声明我的导入,我怎么知道我的IDE比内置版本更喜欢numpy版本? 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. 我检查了numpy.array()和python的内置数组()的文档,看起来它们都接受相同的“list like”参数。

As the commenters have said, you can easily tell which one is being used just by looking at the most recent import statement. 正如评论者所说,你可以通过查看最新的import语句轻松判断哪一个被使用。 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. 但是,如果您感到担心/困惑,您还可以使用Python方便的内置反射功能直接检查函数或类所源自的模块。

For example, the following Python statement: 例如,以下Python语句:

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. 将打印出字符串'numpy.core.multiarray'如果array被从导入numpy包,或串'array' ,如果它是从导入的array封装。

If x.__module__ fails, explore alternatives via dir(x) 如果x.__module__失败,请通过dir(x)探索替代方案

@ShadowRanger raises the good point that some Python objects don't have the __module__ property. @ShadowRanger提出了一些Python对象没有__module__属性的__module__ In particular, if you run just import array , then array is a module and the print(array.__module__) call will fail. 特别是,如果只运行import array ,则array是一个模块, print(array.__module__)调用将失败。 In these kinds of situations you can always discover what reflection information is actually available via the dir() function. 在这种情况下,您始终可以通过dir()函数发现实际可用的反射信息。

dir() is easily my favorite feature of Python. dir()很容易成为Python最喜欢的功能。 For any Python object x , dir(x) prints out the complete list of the attributes of x . 对于任何Python对象xdir(x)打印出x的属性的完整列表。 For example, given that you just ran import array , executing dir(array) would then print out: 例如,假设您刚刚运行了import array ,则执行dir(array)将打印出来:

['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__ . 这表明即使阵列模块缺少__module__ ,如果有其他反射信息可用,例如__name____file__

The best way is probably to keep your namespaces clean, if you can: 最好的方法可能是保持名称空间清洁,如果可以的话:

do: import numpy or import numpy as np , 执行: import numpyimport numpy as np

instead of: from numpy import empty, full, zeros, matrix, arange, asarray, array 而不是: 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. 如果它不适合你,并且不清楚前面提到的是什么, help(array)repr(array)type(array)将会很方便。 (as mentioned in the comments) (如评论中所述)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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