python import numpy,我应该使用numpy(或pylab)作为一个python环境使用`from numpy import *`?...

导入numpy库时使用from numpy import *可能导致名称冲突,影响any、all和sum等内置函数的行为,从而产生难以察觉的错误。这不仅会占用更多内存和加载时间,还可能使代码在不同环境中表现出不一致的行为。建议始终使用import numpy as np来避免这些潜在问题,提高代码可读性和维护性。
摘要由CSDN通过智能技术生成

I use pylab (more specifically numpy) in all of my python programs­. The exceptions are very rare, if any. So far, I have taken the habit of importing numpy in the following way:

from numpy import *

This has the advantage of making it look like numpy was part of python from the beginning. Is there something bad in the fact of importing numpy like this in every script? I mean apart from the fact that every script/program will require a little bit more memory and will take longer to load.

I think always having to write numpy or even np before every function call that comes from numpy (e.g., np.zeros(3)) is tedious because it requires me to know which function comes from numpy and which doesn't. I don't really care that the zeros function comes from numpy or python, I just want/need to use it.

Which notation is better according to you?

解决方案Using from numpy import * changes the behavior of any, all and

sum. For example,

any([[False]])

# True

all([[True, False], [False, False]])

# True

sum([[1,2],[3,4]], 1)

# TypeError: unsupported operand type(s) for +: 'int' and 'list'

Whereas, if you use from numpy import * then values are completely different:

from numpy import *

any([[False]])

# False

all([[True, False], [False, False]])

# False

sum([[1,2],[3,4]], 1)

array([3, 7])

The full set of name collisions can be found this way (thanks to @Joe Kington and @jolvi

for pointing this out):

import numpy as np

np_locals = set(np.__all__)

builtins = set(dir(__builtins__))

print([name for name in np_locals.intersection(builtins) if not name.startswith('__')])

# ['any', 'all', 'sum']

This can lead to very confusing bugs since someone testing or using your

code in a Python interpreter without from numpy import * may see completely

different behavior than you do.

Using multiple imports of the form from module import * can compound the

problem with even more collisions of this sort. If you nip this bad habit in

the bud, you'll never have to worry about this (potentially confounding) bug.

The order of the imports could also matter if both modules redefine the same name.

And it makes it harding to figure out where functions and values come from.

While it is possible to use from numpy import * and still access Python's builtins, it is awkward:

from numpy import *

any([[False]])

__builtins__.any([[False]])

and less readable than:

import numpy as np

np.any([[False]])

any([[False]])

As the Zen of Python says,

Namespaces are a honking great idea

-- let's use more of those!

My advice would be to never use from module import * in any script, period.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值