title: Is there a way to use __str__ in a class but returning a list?
body: <p>When I call a class object, I want it to return a list or any other variable other than string</p>
  <pre><code>class Vec:
      def __init__(self, data) -&gt; None:
          self.data = data #THIS IS A LIST
  
      def __str__(self) -&gt; list:
          return self.data
  
  a = Vec([0,1,2,3])
  print(a) 
  
  #TypeError: __str__ returned non-string (type list)
  </code></pre>
  <p>Instead I get a TypeError, if I set the &quot;self.data&quot; to a string it will work</p>
  <p>Is there any Magic Method for this? Thanks in advance</p>
  <p>here is the full code in case you need to see it all</p>
  <p>note:  I am not done finishing it yet and my problem is that for example if I want to multiply <code>Mat</code> with a <code>Vec</code> I would have to do <code>Mat * Vec.data</code> or <code>Mat * Vec.T</code> (T stands for transposed) which I dont like and I would just like to type <code>Mat * Vec</code> in case I dont need to transpose the vector. Sorry for my messy code and my bad explanation of my problem</p>
  <pre><code>class MatrixMatchError(Exception):
      def __init__(self):
          self.message = &quot;Matrix sizes do not match&quot;
          super().__init__(self.message)
      pass
  
  class MatrixRowColError(Exception):
      def __init__(self):
          self.message = &quot;The number of columns in the first matrix do not equal to the number of rows in the second.&quot;
          super().__init__(self.message)
      pass
  
  class Vec:
      def __init__(self, x, identity=1) -&gt; None:
          self.x = x
          self.data = self.Create(x, 1, identity)
          self.T = self.Transpose()
  
      def Create(self, x, y, identity) -&gt; list:
          vec = []
          for i in range(y):
              vec.append([])
              for j in range(x):
                  vec[i].append(identity)
  
          return vec
  
      def Transpose(self) -&gt; list:
          vec = self.Create(len(self.data), len(self.data[0]), 0)
          for i in range(len(self.data)):
              for j in range(len(self.data[0])):
                  vec[j][i] = self.data[i][j]
  
          return vec
  
  class Mat:
      def __init__(self, x, y=0, identity=1) -&gt; None:
          self.x = x
          if y == 0:
              self.y = x
          else:
              self.y = y
  
          self.data = self.Create(self.x, self.y, identity)
          self.T = self.Transpose()
  
      def __add__(self, data):
          if not self.CheckSize(data):
              raise MatrixMatchError
  
          mat3 = self.data.copy()
          for i in range(len(self.data)):
              for j in range(len(self.data[0])):
                  mat3[i][j] = self.data[i][j] + data[i][j]
  
          return mat3
  
      def __sub__(self, data):
          if not self.CheckSize(data):
              raise MatrixMatchError
  
          mat3 = self.data.copy()
          for i in range(len(self.data)):
              for j in range(len(self.data[0])):
                  mat3[i][j] = self.data[i][j] - data[i][j]
  
          return mat3
  
      def __mul__(self, data):
          if not self.CheckRowCol(data):
              raise MatrixRowColError
  
          mat3 = self.Create(len(data[0]), len(self.data),  0)
  
          for i in range(len(self.data)):
              for j in range(len(data[0])):
                  for k in range(len(self.data[0])):
                      mat3[i][j] += self.data[i][k] * data[k][j]
  
          return mat3
  
      def Create(self, x, y, identity) -&gt; list:
          mat = []
          for i in range(y):
              mat.append([])
              for j in range(x):
                  mat[i].append(0)
  
          if x == y:
              for i in range(x):
                  mat[i][i] = identity
  
          return mat
  
      def Transpose(self) -&gt; list:
          mat = self.Create(len(self.data), len(self.data[0]), 0)
          for i in range(len(self.data)):
              for j in range(len(self.data[0])):
                  mat[j][i] = self.data[i][j]
  
          return mat
  
      def CheckSize(self, data):
          if len(self.data) != len(data) or len(self.data[0]) != len(data[0]):
              return False
          return True
  
      def CheckRowCol(self, data):
          if len(data) != len(self.data[0]):
              return False
          return True
  
      def Transform(self):
          pass
  
      def Scale(self):
          pass
  
      def Rotate(self):
          pass
          
  
  mat1 = Mat(2)
  mat1.data = [[4,5, 4],
              [9,3, 7],
              [7,1, 3]]
  
  vec = Vec(3)
  print(mat1 * vec.T)
  </code></pre>