Chapter 12 Classes

12.1 Classes and Instances

This chapter discusses classes, Python’s mechanism for defining custom types, showing how object construction, attributes, methods and operators can be defined for a new type. Classes are the core concept in object oriented programming. They represent types. In OOP terminology, objects that belong to a type are called instances of the type. For example, 1 and −2 are instances of integers, while ‘abc’ and ‘greetings’ are instances of strings.

This correlation between built-in types and their objects can be generalized, and used for modeling custom types, namely classes. For example, John’s bank account is an instance of the account type, and Mary’s pet dog is an instance of the dog type. Further, types can have hierarchies. For example, the dog type is a specific sub type of the canine type, which has the wolf type as another sub type. The abstraction of types and instances is central to the concept of object oriented programming, which has fundamental influences on programming languages, making code easier to understand and maintain.

© Copyright 2024 GS Ng.

Next Section - 12.2 Classes and Attributes