Home > database >  Call a method variable from outside the class
Call a method variable from outside the class

Time:07-17

I'm trying to make a class that contains an array and then name the columns of the array with instance variables and return the value of the cell in that row/column externally by filling in the row value from calling the method index_value.

However I get this error every time I try to call it.

AttributeError: 'function' object has no attribute 'value'

I've tried

import numpy as np
class stuff:
    array = np.zeros((5,5), dtype = float)
    
    def index_value(self, arr_index_number = 0):
        self.value = self.array[arr_index_number,0]   
        self.value_2 = self.array[arr_index_number,1]   
        
    def index_number(self, n = 0):
        return  n

stuff = stuff()
stuff_index_value = stuff.index_value
print(stuff_index_value.value(1))

and with an init

import numpy as np
class stuff:
    
    def __init__(self):
        self.array = np.zeros((5,5), dtype = float)
    
    def index_value(self, arr_index_number = 0):
        self.value = self.array[arr_index_number,0]   
        self.value_2 = self.array[arr_index_number,1]   
        


stuff = stuff()
stuff_index_value = stuff.index_value
print(stuff_index_value.value(1))

I've tried

print(stuff_index_value.value(1))

and

print(stuff.value(1))

What am I doing wrong? :(

To clarify what I want to do, I want to store every column in the array as its own variable. Each column represents a different category of data so I need them to be saved to their own variable. Then I want to be able to call the value of the row/column by using the method and inputting the row number. so in

self.value_2 = self.array[self.arr_index_number,1] 

if arr_index_number == 1 then the value that would be returned would be the value of self.array[1,1]. In order to change the value of arr_index_number I need to call it from the method 'index_value'.

So if self.array[1,1] = 5 then I would need to call it with the equivalent of

stuff.index_value.value_2(1)

where (1) is the arr_index_number in the method in order to get that value. The trouble here is I can't just call the method and be done it with, I need to call the variables within the method and their transformed values. I can't figure out the correct syntax to do this.

CodePudding user response:

Create a method called "_init_" in your class. This is a special method in python which is called whenever a new instance of the class is created. In that method you can store your self.array variable. Once you have done that, you can call that variable from outside the class by writing:

class.variable

Example

class Test:
    def __init__(self):
        self.variable = "Wow"

test = Test()
print(test.variable)

>> Wow

You can get more info on this site: udacity.com P.S.=Hope my english is clear enough

CodePudding user response:

I firstly tried to create a generic class:

class Test:
    def __init__(self, value = None):
        # here store all the variable of the class (also the variables you will use in the class methods below)
        self.value = value

    # function that changes the value of the variable "self.value"
    def change_value(self, new_value):
        self.value = new_value

# first argument of __init__ method that sets self.value = 1
# Note that if I didn't write anything, the default value would be None (if I wrote test = Test())
test = Test(1)
test.change_value(2) # Calling the method that changes the value inside the class
print(test.value) # accessing the value of "self.value"

Then I tried to create a better version of your class, but I don't really know what you want to do with it, therefore I'm not sure if it suits your needs.

class Stuff:
    def __init__(self):
        self.array = np.zeros((5,5), dtype = float)
        self.value = None
        self.value_2 = None
        # self.n = [something] (see comments on index_number function)

    def index_value(self, arr_index_number = 0):
        # now you can change the value and actually save it inside the class
        # if you don't declare the variable inside __init__, you won't be able to access the variable outside the class
        self.value = self.array[self.index_number(arr_index_number),0]   
        self.value_2 = self.array[self.index_number(arr_index_number),1]   


    # I didn't really understand why do you need a function to get the number index
    # Do you need to store "n" as a variable in the class?
    # You can again save it in the __init__ function
    def index_number(self, n = 0):
        return  n

Hope I was clear.
If you need more clarification, don't hesitate to ask again!

  • Related