Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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,

share|improve this question
    
See the difference between nparray.array()[i] and nparray.array[i]? –  jazzpi Nov 27 '13 at 18:51
    
Could you please give me some references for "nparray.array()[i]"? –  user3040845 Nov 27 '13 at 18:58
    
Sorry for writing a wall of text, but please take a moment and read my answer @user3040845 –  Dunno Nov 27 '13 at 19:08

2 Answers 2

up vote 0 down vote accepted

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
share|improve this answer

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

share|improve this answer
    
Yes, but I want to generate a syntex similar to the code that I am reviewing: PromotionIdData.array()[ID,t]=... –  user3040845 Nov 27 '13 at 18:56
    
Edited my reply to address your clarification –  Javier Nov 27 '13 at 19:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.