我是Python和OOP概念的新手,我无法理解某些东西,比如为什么某些函数会改变原始对象而有些则不会。为了更好地理解它,我在下面的代码片段中将注意力放在了混淆之处。任何帮助表示赞赏。谢谢。对象在Python中更改
from numpy import *
a = array([[1,2,3],[4,5,6]],float)
print a
array([[ 1., 2., 3.],
[ 4., 5., 6.]]) ### Result reflected after using print a
a.reshape(3,2)
array([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]]) ### Result reflected on IDE after applying the reshape function
print a
array([[ 1., 2., 3.],
[ 4., 5., 6.]]) ### It remains the same as original value of "a", which is expected.
a.fill(0)
print a
[[ 0. 0. 0.]
[ 0. 0. 0.]] ### It changed the value of array "a" , why?
#############
type(reshape) ### If i try to find the type of "reshape" , i get an answer as "function" .
<type 'function'>
type(fill) ### I get a traceback when i try to find type of "fill", why?
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
type(fill)
NameError: name 'fill' is not defined
我的问题是:
1)我要如何知道哪些功能(S)(考虑“补”是一个功能)将改变我原来的对象值(在我情况下它的“a”)?如果“fill”是一个函数,那么为什么它改变了对象“a”的原始值?
3)为什么我使用类型(填充)时得到回溯?
给定函数可以改变或者不是对象输入对象值。在NumPy中,许多函数都带有'out'参数,它告诉函数将答案放在这个对象中... – 2014-09-25 09:53:23
@Saullo Castro,谢谢你的回复,如果是这样的话,对于像我这样的人来说,是新的),学习如何知道哪个函数有“out”参数,谁没有,有没有看到它,或者在学习语言后变得直观。顺便说一下,“out”参数是什么? – PKumar 2014-09-25 10:02:42
[检查np.multiply](http://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html)函数,例如...并查看'out'参数 – 2014-09-25 10:07:28