Classes in Python
In this tutorial we'll learn about classes in Python. We'll learn what is class, object and object-oriented programming. Python has support of object-oriented programming from the beginning (unlike C language).
In previous tutorials when we wanted to model some entity from the real world, we could use variables and functions. Object-oriented programming binds variables and functions in one entity.
Object-oriented programming allows easily model (represent) real world objects (or some abstractions) in programming languages. Two pillar concepts of object-oriented programming are classes and objects.
Class is like a blueprint and object is a real thing made from blueprint. So, first you need to define the class.
Class definition in Python
Let's think of a game where are many similar objects like tanks. All objects can move and fire (make actions). Also all this tanks will be of one model. Let's create the class for such tanks in Python programming language:
Class definition starts with keyword class after which we need to write name of a class. It's usual to write name of the classes with capital letters. After class name we put colon and rest definition should be indented.
model is class attribute or class variable. Class variables are shared between all objects. move and fire are called class methods. Internally they are just common functions. Every method of a class must receive one parameter - self, we'll return to it soon.
At this point we defined the class Tank which has one class variable and two methods. Nothing happens during moving and firing - we just added pass statement as placeholder for future code. Now let's find out what are objects.
Objects of classes
After class is defined, we can create it's objects (instances). To create class object, you need to put name of a class with brackets:
We created three tanks. We can get class variable or call methods with . operator:
First, we write name of object, then dot, then name of class variable or method call. Pay attention that we can get class variable from class itself and from any object of the class.
When we call any class method, the object itself is passed inside the method as parameter self. So inside methods you can address object that called method.
Our tanks does nothing yet while they moving or firing. Let's change it.
__init__ - class constructor in Python language
When object is created there are brackets after class name. We know that brackets are used in functions. Actually during object creation the special function (constructor) is called. It's name is __init__. It's not required to define it inside class definition, but you may do it. And you must do it if you want to do specific actions during object creation. Let's add to the objects additional data members and allow user to define coordinates when object is created:
We added constructor to class definition. Besides self> it accepts initial coordinates. When we create objects we need to pass to constructor actual values.
All objects have coordinates, number of ammunition and fuel: x, y, fuel, ammunition are called data members of a class. Each object has it's own set of attributes (x,y, fuel, ammunition)
When one of tanks moves or fires: we change attributes fuel and ammunition of the object. Our tanks can't do nothing fancy. When they move - they just consume fuel and when they fire - the ammunition is reduced. It's very primitive model, let's be patient and soon our tanks will be capable of much more.
You need to remember these about classes:
- Class is like a blueprint where you define different attributes (data members and class variables) and behavior (methods) of class objects.
- You can create as many class objects (instances of class) as you need. They all will share same class variables and each of them will have unique data members. Each object is capable to call class methods.
- self variable is used to address object that has called current method. - Constructor function __init__ is called during creation of object and you can use it to initialize data members.