Create your own game
eng   рус

Classes in Python

Previous tutorial: Functions in Python
Next tutorial: SDL2 in Python Programming Language

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).

Object-Oriented Programming

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 Tank: model = "Mark I" def move (self): pass def fire (self): pass

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:

t1 = Tank() t2 = Tank() t3 = Tank()

We created three tanks. We can get class variable or call methods with . operator:

t1.move() t2.move() print(Tank.model) print(t1.model)

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:

class Tank: model = "Mark I" def __init__(self, init_x, init_y): self.x = init_x self.y = init_y self.fuel = 100 self.ammunition = 20 def move (self): self.fuel -= 1 def fire (self): self.ammunition -= 1 # Now let's create objects t1 = Tank(10, 10) t2 = Tank(0, 0) t3 = Tank(34, 23) print(t1.x) print(t2.x) t1.move() print(t1.ammunition)

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.


No comments yet