Confused as to why this is not working - Building Useful Classes


#1

I’m getting the error :

Traceback (most recent call last):
  File "python", line 12, in <module>
TypeError: 'Point3D' object is not callable

and I don’t know why. It’s probably some silly error but I’ve looked over my code several times and I still can’t find it.

Here’s my code:

class Point3D(object):
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z
    
  def __repr__():
    return "(%d, %d, %d)" % (self.x, self.y, self.z)
  
  
my_point = Point3D(1, 2, 3)
print my_point()

Thanks for the help in advance!! :slight_smile:

***Update: I changed the code and now it works but I don’t understand why it didn’t work before could someone explain. Here’s my code:

class Point3D(object):
  def __init__(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z
    
  def __repr__(self):
    return "(%d, %d, %d)" % (self.x, self.y, self.z)
  
  
my_point = Point3D(1, 2, 3)
print my_point

#2

was raised because this line,

attempts to invoke (call) a function. my_point is an instance, not a function.

That line works because it prints the object instance using the __repr__ method in the class.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.