top of page
Search
Writer's pictureRohan Roy

Object Oriented Programming in Python

Original post: https://medium.com/@rohanrony/object-oriented-programming-in-python-b927244e5ac5


Classes, attributes and methods

In object oriented programming, we have objects. Functions are methods and variables are attributes of the object. There are also instances of the object.

e.g. of an object.

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity
   def number_of_wheels(self):
        return self.number_of_wheels
   def set_number_of_wheels(self, number):
        self.number_of_wheels = number
tesla = Vehicle(4, 'Petrol', 6, 150)

Here, tesla is an instance of the object Vehicle with all the attributes defined



Encapsulation — Hiding information

In python there is no hidden variable in reality. You can set it to private by using _variable and you can keep it hidden by __variable.

see the example code:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank,    seating_capacity):
        self.number_of_wheels = number_of_wheels
        self._type_of_tank = type_of_tank
        self.__seating_capacity = seating_capacity
   def get_number_of_wheels(self):
        return self.number_of_wheels
   def set_number_of_wheels(self, number):
        self.number_of_wheels = number
   def get_type_of_tank(self):
        return self._type_of_tank
   def set_type_of_tank(self, number):
        self._type_of_tank = number
   def get_seating_capacity(self):
        return self.__seating_capacity
   def set_seating_capacity(self, number):
        self.__seating_capacity = number
        
tesla=Vehicle(4,'cool',6)
dir(tesla)

output:

['_Vehicle__seating_capacity',
 '__doc__',
 '__init__',
 '__module__',
 '_type_of_tank',
 'get_number_of_wheels',
 'get_seating_capacity',
 'get_type_of_tank',
 'number_of_wheels',
 'set_number_of_wheels',
 'set_seating_capacity',
 'set_type_of_tank']

Printing attributes..

print tesla.number_of_wheels
print tesla._type_of_tank
print tesla._Vehicle__seating_capacity
print tesla.__seating_capacity

output: 

4
cool
6
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-67-9c17b31fc22e> in <module>()
      2 print tesla._type_of_tank
      3 print tesla._Vehicle__seating_capacity
----> 4 print tesla.__seating_capacity

AttributeError: Vehicle instance has no attribute '__seating_capacity'


Inheritance

We define an object Car that inherits attributes and methods from object Vehicle and object PetrolCar that inherits from Car

see code:

class Vehicle:
    def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
        self.number_of_wheels = number_of_wheels
        self.type_of_tank = type_of_tank
        self.seating_capacity = seating_capacity
        self.maximum_velocity = maximum_velocity
def number_of_wheels(self):
        return self.number_of_wheels
def set_number_of_wheels(self, number):
        self.number_of_wheels = number
def __repr__(self):
        return "Vehicle \nNo_Wheels=%s, Tank_Type=%s, Seat_cap=%s,   Max_vel=%s" % (self.number_of_wheels, self.type_of_tank, self.seating_capacity, self.maximum_velocity)
class Car(Vehicle):
    def __init__(self, type_of_tank, seating_capacity, maximum_velocity):
        Vehicle.__init__(self, 4, type_of_tank, seating_capacity, maximum_velocity)
class PetrolCar(Vehicle):
    def __init__(self, seating_capacity, maximum_velocity):
        Vehicle.__init__(self, 4, 'Petrol', seating_capacity, maximum_velocity)
Alto = PetrolCar(4, 150)
print Alto
Alto.get_number_of_wheels()

output:

Vehicle 
No_Wheels=4, Tank_Type=Petrol, Seat_cap=4, Max_vel=150
4


Printing objects

class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
    def __repr__(self):
        return "Test a:%s b:%s" % (self.a, self.b)
 
    def __str__(self):
        return "From str method of Test: a is %s," \
              "b is %s" % (self.a, self.b)
# Driver Code        
t = Test(1234, 5678)
print(t) # This calls __str__()
print([t]) # This calls __repr__()

output:

From str method of Test: a is 1234,b is 5678
[Test a:1234 b:5678]



Sources:

6 views0 comments

Commentaires


bottom of page