Building useful classes

Chances are, you won’t be designing Car classes in the real world anytime soon. Usually, classes are most useful for holding and accessing abstract collections of data.

One useful class method to override is the built-in __repr__() method, which is short for representation; by providing a return value in this method, we can tell Python how to represent an object of our class (for instance, when using a print statement).



Define a Point3D class that inherits from object

Inside the Point3D class, define an __init__() function that accepts self, x, y, and z, and assigns these numbers to the member variables self.x, self.y, self.z

Define a __repr__() method that returns "(%d, %d, %d)" % (self.x, self.y, self.z). This tells Python to represent this object in the following format: (x, y, z).

Outside the class definition, create a variable named my_point containing a new instance of Point3D with x=1, y=2, and z=3.

Finally, print my_point.

Folder Icon

Take this course for free

Already have an account?