We wish to develop an application to keep track of the isp (individual study program) of a student and to do some calculations on it (such as the total number of credits over a year, a list with names of all courses sorted from most to least credits, …).
Thus, we wish to keep track of information about a course in the first place. From the OO point of view, we will therefore create a class Course that keeps track of all the information of a course. It is customary to have class names start with a capital letter. A class is a description of what properties objects of this class (specific courses in this case, for example the course named “Programming 1” which has 6 credits) have, how the objects of these classes can be created and what methods can be applied to these course objects. Instead of properties of an object, we sometimes talk about the attributes of an object.
What are the properties we want to keep track of about a course? For a course, we are interested in the number of credits of a course (since we have want to be able to calculate the sum of the credits of all courses in the study program) and the name of a course (since we want to generate a list of course names for a study program). In addition, we will need to be able to create new course-objects based on a given name and given credits, and wish to be able to access the name and number of credits of a specific course-object.
What are the properties we wish to keep track of a student? For this application, we are only interested in the student’s study program. So the only property is a collection of course objects that contains the student’s study program. When a student object is created we do not need any additional information. We will need a method to add a new course object to the collection. We will need a method to calculate the total number of credits of all course objects in the collection. And we will need a method to generate a list of strings of all the names of the course objects in the study program sorted in descending order of credits.
In the object-oriented world, every distinguishable entity belongs to a class. A class is a generic model for a group of entities. The class describes all the attributes (or properties) that the entities have in common, and describes the methods that the class provides that allow the world outside the class to influence the class.
By itself, a class is not an entity, but a description of the entities of that class. An entity that belongs to a class is called an “object.” The terminology is that an object is an “instance” (sometimes: “instantiation,” but that is not correct English) of a particular class. A class describes attributes, but an object that is an instance of the class assigns values to the attributes. And although a class describes the methods it supports, you can only execute a method for an object that is an instance of the class.
A class is a data type, an object is a value.
Most object-oriented programming languages have some basic data types, and allow you to define classes, which amounts to defining new data types. This was also true of Python up to version 2. Since Python 3, however, every data type is a class.
You can see this in part by the way a large number of functionalities of basic data types are implemented in Python
as methods. A method is always called via the syntax <variable>.<method>()
, unlike functions, which are
called as <function>( <variable> )
. The fact, for example, that if you want to create a lowercase version of a string, you
effectuated by means of <string>.lower()
already indicates that a string is an instance of a class.
But not only strings are instances of classes: integers and floats are too. They even have methods, but these are rarely called explicitly. called. Methods are often called implicitly though, for example, if you add two numbers with \(+\), that is actually the invocation of a method.