11. Building Useful Classes


#1

Why does the following code return an error?

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

The error message:

script.py

1
2
3
4
5
6
7
8
9
10
11
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
Traceback (most recent call last):
File "python", line 11, in
File "python", line 8, in repr
TypeError: %d format: a number is required, not tuple


#2

You need to remove the commas after your variable settings for self.x and self.y


#4

Thanks, that was it! :slight_smile:


#5

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