English English

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/ard-site/templates/ardsite/library/Designer/Content/SingleArticle.php on line 198

Object Oriented Programming (OOP) in Python

This is a brief introduction to Object Oriented Programming (OOP) in the programming language Python. You need to have some knowledge about OOP, because this tutorial does cover only how to use OOP in Python.


Use the statement class followed by the class name to create a new class in Python. The class description (written in single quotes) and all the class elements (instance variables, etc) are written (intendet) after the class statement.
This is a basic example of a class:

class Student:
  'This is the class for all students of the high school'
   studentsNumber = 0
   def __init__(self, firstName, lastName):
     self.firstName = firstName
     self.lastName = lastName
     Student.studentsNumber +=1

   def showStudent(self):
      print "First Name: ", self.firstName, " - Last Name: ", self.lastName

Variable studentsNumber: This is a class variable. Its value is shared of all instances of this class. The function showStudent() displays this variable, which has the numbers of all created students (instances of this class).

Method __init__: This is the class constructor which will be called every time when a new instance of this class is created. It does initialize the instance and you can add your custom code too in the class constructor.

Every method must have self as an argument (automatically created by Python) and instance variable are accessed by self.

Getter and setter methods

To ensure data encapsulation it is recommended to use properties. The getter and setter methods come always after the class constructor.

class Student:

   def __init__(self, firstName, lastName):
     self.firstName = firstName
     self.lastName = lastName

    def firstName(self):
          return self.__firstName

    def firstName(self, firstName):
          self.__firstName = firstName

    def lastName (self):
          return self.__lastName

    def firstName(self, lastName):
          self.__lastName = lastName

Getter method: The method which gets the value is decorated with @property
Setter method: The method which sets a value is decorated with @function.setter. You have to use the same name in the function name and in this decorater statement.

Instance Objects

An instance of a class is created using the defined class name and in the __init__ method defined arguments.

student1 = Student("Alex","Alexson")
student2 = Student("Jenny","Maiersoner")

We created now two objects of the Student class. You can access its attributes as following:


The first two lines show how to access methods. The last line shows how to access a public variable.


For this example a school has N students and M teachers. The class School is the superclass (base class) and the classes Student and Teacher are subclasses (derived classes).

class School:
	def __init__(self, schoolname):
		self.schoolname = schoolname
	def displayInformation(self):
		print("School name: ", self.schoolname)

class Student(School):
    'Student of a school'
	def __init__(self, firstname, lastname, schoolname):
		School.__init__(self, schoolname)
		self.firstname = firstname
		self.lastname = lastname

	def displayInformation(self):
		print("First name: ", self.firstname, " - Last name: ", self.lastname)
class Teacher(School):
    'Teacher of a school'
	def __init__(self, firstname, lastname, degree, schoolname):
		School.__init__(self, schoolname)
		self.firstname = firstname
		self.lastname = lastname
		self.degree = degree

	def displayInformation(self):
		print("First name: ", self.firstname, " - Last name: ", self.lastname, " - Degree: ", self.degree)

To inherit a class you have to write the class name in round brackets. You have to call the constructor of the superclass in the constructors of the sub classes, which gets the variables that have to be initialized in the superclass. This is not done automatically, so you have to program that.
Python searchs first for methods (in this example: displayInformation) in subclasses, then in the superclass if it did not find any in the subclasses.
If you access the method in this example displayInformation through a declared class, then you will have access to both methods in the subclass and superclass if you created a new Teacher.

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.