O'Reilly logo

Data Structures and Algorithms Using Python by Rance D. Necaise

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix D. Classes

Python supports both the procedural and object-oriented programming paradigms. Whereas the procedural paradigm is focused on the creation of functions, the object-oriented paradigm is centered around the use of objects and classes. An object is a software entity that stores data. A class s the blueprint that describes the data stored in an object and defines the operations that can be performed on the object. Objects are created or instantiated from classes, and each object is known as an instance

The Class Definition

Python, like all object-oriented programming languages, allow programmers to define their own classes. A class definition is a compound statement consisting of a header and a body. The class header contains the keyword class followed by an identifier used to name the class. The body of the class definition contains one or more method definitions, all of which must be indented to same indentation level.

Suppose we want to define a class to represent a point or coordinate in the two-dimensional Cartesian coordinate system. The objects will need to store two values, one for the x-coordinate and one for the y-coordinate. We will also have to decide what operations we want to be able to perform on the objects created from the new class. We can begin with a framework for the new class: of the class from which it was created.

class Point :
   # The methods are defined in the body one after the other.

Notice that our class is named Point, which starts with an uppercase ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required