Class variables not callable?

Question:

I’ve stumbled on what is probably a simple issue but I struggle to think of a solution.
If I try to make a class variable that equates to a number or an array index they come up with these errors

'int' object is not callable

and

'numpy.ndarray' object is not callable

class Do_something:
   def __init__(self,number = 0):
      self.number = number
      self.result = 1 + self.number

something = Do_something()
print(something.result(1))

import numpy as np

class Do_something_else:

   def __init__(sel):
      self.arr = np.zeros([5,5])
      self.index = self.arr[0]

something = Do_something_else()
print(something.index())

This seems odd since I can set variables equal to ints and arrays elsewhere.
What am I missing and what is the solution?

Asked By: thatoneguy

||

Answers:

In both piece of code result and index are not methods, they are instance valiables.
To make this code work it should look like this:

class Do_something:
   def __init__(self,number = 0):
      self.number = number
      self.result = 1 + self.number

something = Do_something(1)
print(something.result)

and

class Do_something_else:

   def __init__(self):
      self.arr = np.zeros([5,5])
      self.index = self.arr[0]

something = Do_something_else()
print(something.index)

Also pay attention that there ia s misspelling in def __init__(sel): line in Do_something_else class. It should be def __init__(self):

Answered By: Masha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.