Characteristics of Object Oriented programming in Python Part-2

In part-1 of this segment, we discussed general characteristics of Object-oriented programming language. In this post, we’ll discuss these characteristics in detail and their implementation in Python.

In an object-oriented python program, you can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as Encapsulation.

Encapsulation allows programmers better control of how data flows in their programs, and it protects that data. Encapsulation also makes objects into more self-sufficient and independently functioning pieces.

Private methods

We create a class truck which has two methods:  drive() and updateSoftware().  When a truck object is created, it will call the private methods __updateSoftware().  

This function cannot be called on the object directly, only from within the class.

#!/usr/bin/env python
 
class Truck:
 
    def __init__(self):
        self.__updateSoftware()
 
    def drive(self):
        print 'driving'
 
    def __updateSoftware(self):
        print 'updating software'
 
redtruck = Truck()
redtruck.drive()
#redtruck.__updateSoftware()  not accesible from object.

This program will output:

updating software
driving

Encapsulation prevents from accessing accidentally, but not intentionally.

The private attributes and methods are not really hidden, they’re renamed adding “_Truck” in the beginning of their name.

The method can actually be called using redtruck._Truck__updateSoftware()

Private variables

Variables can be private which can be useful on many occasions. A private variable can only be changed within a class method and not outside of the class.

Objects can hold crucial data for your application and you do not want that data to be changeable from anywhere in the code.
An example:

#!/usr/bin/env python
 
class Truck:
 
    __maxspeed = 0
    __name = ""
 
    def __init__(self):
        self.__maxspeed = 200
        self.__name = "Supertruck"
 
    def drive(self):
        print 'driving. maxspeed ' + str(self.__maxspeed)
 
redtruck = Truck()
redtruck.drive()
redtruck.__maxspeed = 10  # will not change variable because its private
redtruck.drive()

If you want to change the value of a private variable, a setter method is used.  This is simply a method that sets the value of a private variable.

#!/usr/bin/env python
 
class Truck:
 
    __maxspeed = 0
    __name = ""
 
    def __init__(self):
        self.__maxspeed = 200
        self.__name = "Supertruck"
 
    def drive(self):
        print 'driving. maxspeed ' + str(self.__maxspeed)
 
    def setMaxSpeed(self,speed):
        self.__maxspeed = speed
 
redtruck = Truck()
redtruck.drive()
redtruck.setMaxSpeed(320)
redtruck.drive()

To summarise, in Python there are:

TypeDescription
public methods

Accessible from anywhere


private methods

Accessible only in their own class. starts with two underscores


public variables

Accessible from anywhere


private variables

Accessible only in their own class or by a method if defined. starts with two underscores

Leave a Reply