简体   繁体   中英

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. Following is the syntax that I have:

self.PromotionIdData.array()[self.ID,t] = ...

PromotionIdData is a two-dimensional numpy array. 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 ).

Once you have a numpy array object you can access the elements using square brackets without any additional notes:

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)

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:

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). For this type of constructions you might want to take a look at the notions of getters and setters

Your *test *class has 3 attributes: price, cost and volume.

Numpy module, or to simplify, class, has its own attribute array.

Numpy and test are separate classes, and you're confusing them. In your __init__ you call the function np.array() and you assign the output to test.price . Now let's take a minute and think what exactly was assigned to test.price . It's nothing more than a numpy array, and test.price behaves exactly the same as any other numpy array would. 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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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