[英]Array in Python Object Oriented
I am kind of new in Object Oriented programming. 我是面向对象编程的新手。 I am reviewing a code and there is a part that I have difficulty to understand.
我正在检查代码,有些部分我很难理解。 I do appreciate if you can help me.
如果您能帮助我,我非常感谢。
I have a code in which we have numpy array and then it seems "array" is used as attribute or method for the the numpy array but I am not sure how it does work. 我有一个代码,其中有numpy数组,然后似乎将“ array”用作numpy数组的属性或方法,但是我不确定它如何工作。 Following is the syntax that I have:
以下是我的语法:
self.PromotionIdData.array()[self.ID,t] = ...
self.PromotionIdData.array()[self.ID,t] = ...
PromotionIdData is a two-dimensional numpy array. PromotionIdData是二维numpy数组。 But I can not generate the similar syntex for myself.
但是我无法为自己生成类似的语法。 here is my code:
这是我的代码:
import numpy as np
from array import *
class test:
def __init__ (self):
self.price=np.array([10,20,30])
self.cost=20
self.volum=2
a=test()
print getattr(a,'price').array[1]
But my code does not work. 但是我的代码不起作用。 I got the error indicating "array" is not an attribute.
我收到错误消息,指出“数组”不是属性。
Thanks, 谢谢,
You don't need to import array
, as it is a numpy class and you are already importing numpy (and indeed you call array with np.array
). 您不需要导入
array
,因为它是一个numpy类,并且您已经在导入numpy(实际上,您使用np.array
调用array)。
Once you have a numpy array object you can access the elements using square brackets without any additional notes: 一旦有了numpy数组对象,就可以使用方括号访问元素,而无需任何其他注释:
a = np.array([1,2,3,4])
print a[1]
Also, to access an attribute of your class you only need to use the dot syntax, I think your test should read something like this: 另外,要访问类的属性,您只需要使用点语法,我认为您的测试应读取如下内容:
print a.price[1]
(Edited after the OP clarification) (在OP澄清后编辑)
If you want to be able to call array()
in your class, you need to define an array method that returns the attribute you want: 如果要能够在类中调用
array()
,则需要定义一个返回所需属性的数组方法:
class test():
def __init__(self):
self.price=np.array([10,20,30])
self.cost=20
self.volum=2
def array(self):
return self.price
then you can do a.array()[1]
(note that you don't need to explicitly indicate the price attribute anymore). 那么您可以执行
a.array()[1]
(请注意,您不再需要明确指出price属性)。 For this type of constructions you might want to take a look at the notions of getters and setters 对于这种类型的构造,您可能需要看一看getter和setter的概念。
Your *test *class has 3 attributes: price, cost and volume. 您的* test *类具有3个属性:价格,成本和数量。
Numpy module, or to simplify, class, has its own attribute array. Numpy模块(或简化类)具有自己的属性数组。
Numpy and test are separate classes, and you're confusing them. Numpy和test是单独的类,您会混淆它们。 In your
__init__
you call the function np.array()
and you assign the output to test.price
. 在
__init__
,调用函数np.array()
然后将输出分配给test.price
。 Now let's take a minute and think what exactly was assigned to test.price
. 现在,花一点时间,想一想到底给
test.price
分配了test.price
。 It's nothing more than a numpy array, and test.price
behaves exactly the same as any other numpy array would. 它不过是一个numpy数组,而
test.price
行为与任何其他numpy数组完全相同。 Classes and objects aren't black magic: variables stored there behave in a very intuitive way! 类和对象不是万能的:存储在其中的变量的行为非常直观! This was hard for me to imagine at first too, so I know where you stand.
一开始我也很难想象,所以我知道你的立场。 One more thing to remember: classes have their own namespaces, which means that
test.var
is independent from var
and they don't interfere with each other in any way. 需要记住的一件事:类具有自己的名称空间,这意味着
test.var
与var
独立,并且它们不会以任何方式相互干扰。 You'll learn to appreciate namespaces sooner or later ;) 您将早晚学会欣赏名称空间;)
A couple of more examples: 还有更多示例:
def __init__(self):
dict={'a': 123}
list=[1,2,3,4]
var='spam'
var='eggs and spam'
>>>test.dict['a']
123
>>>test.list[2]
3
>>>test.var
spam
>>>var
eggs and spam
As to your example of self.PromotionIdData.array()[self.ID,t] = ...
That's a bit higher level of object oriented programming. 关于
self.PromotionIdData.array()[self.ID,t] = ...
这是面向对象编程的更高级别。 Inside class definitions you can define functions, or rather methods, and call them the way you did in your example. 在类定义内部,您可以定义函数或方法,并以示例中的方式进行调用。 Like so:
像这样:
class test():
def array(self): #remember to always pass self to a function inside your class
return [1,2,3,4,5]
>>>t=test()
>>>t.array[2]
3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.