Object oriented programming in python:

Author: Dr. Rahul Remanan

CEO and Chief Imagination Officer

Moad Computer

  • Objects are an encapsulation of variables and functions into a single entity.
  • Objects get their variables and functions from classes.
  • Classes are essentially a template to create your objects.

Part 01 -- Functions in python:

Just add two numbers:


In [1]:
a = int(input())
b = int(input())


56
65

In [2]:
print (a+b)


121

Explicitly calling garbage collection to free-up working memory:


In [3]:
import gc

del a
del b
gc.collect()


Out[3]:
221

A very basic python function:

  • A function to add two numbers.
  • Uses positional argument.

In [4]:
def add_function(a,b):
  print ('Calculating the sum of: ' + str(a) + " , " + str(b))
  return (a+b)
  • The input() helps user pass an input value.

In [5]:
input_1 = int(input())
input_2 = int(input())


56
65

In [6]:
result = add_function(input_1, input_2)


Calculating the sum of: 56 , 65

In [7]:
print (result)


121

Explicitly freeing-up working memory and calling garbage collector:

  • Object oriented programing is memory efficient.

In [8]:
import gc

del input_1
del input_2
del result

gc.collect()


Out[8]:
115

Another basic python function:

  • This is a function to add two numbers.
  • Uses keyword arguments instead of positional arguments.
  • The function takes two decimal values instead of two integer values in the function above.
  • Explicit error messages.

In [9]:
def add_function(input_1=None, input_2=None):
  if input_1 and input_2 !=None:
    print ('Calculating the sum of: ' + str(input_1) + " , " + str(input_2))
    return (a+b)
  else:
    print ('Nothing to do here ...')
    print ('Ensure add_function passes two keyword variables: input_1 and input_2 ...')

In [10]:
a = float(input())
b = float(input())


3.14
5.65

In [11]:
result = add_function(input_2=b, input_1=a)


Calculating the sum of: 3.14 , 5.65

In [12]:
print (result)


8.790000000000001

Explicit error message looks like this:


In [13]:
result = add_function(input_1=a)


Nothing to do here ...
Ensure add_function passes two keyword variables: input_1 and input_2 ...

Part 02 -- Classes in python:

Python is an “object-oriented programming language.” This means almost all the code is implemented using a special construct called classes. Programmers use classes to keep related things together. This is done using the keyword “class,” which is a grouping of object-oriented constructs.

Define a very basic class in python:

  • Contains a variable called "some_variable".
  • Contains a function called "some_function".

In [14]:
class some_class:
    some_variable = "Some string inside python class."
    def some_function(self):
        return ("This is a message inside the class.")

Assign an object to the class:

  • To assign the above class(template) to an object(variable), do the following:

In [15]:
some_object = some_class()

Accessing object variables:

  • To access the variable inside of the newly created object object_name.variable_name.

In [16]:
print(some_class.some_variable)


Some string inside python class.

In [17]:
print(some_object.some_variable)


Some string inside python class.

In [18]:
some_object.some_variable = 'This is the modified variable.'

In [19]:
print (some_object.some_variable)


This is the modified variable.

Accessing object functions:

  • To access the function inside the newly created object the syntax is: object_name.function_name().

In [20]:
print(some_object.some_function())


This is a message inside the class.

Importance of object oriented programing:

  • Object oriented programing helps create multiple different objects.
  • They are of the same class with the same variables and functions defined.
  • Each of those objects contain independent copies of the variables defined in the class.
  • If another object with the "some_class" class is defined, then the string in the variable can be changed as follows:

In [21]:
first_object = some_class()
second_object = some_class()

In [22]:
second_object.some_variable = "Modified variable string for some_class."

In [23]:
print(first_object.some_variable)
print(second_object.some_variable)


Some string inside python class.
Modified variable string for some_class.

Accessing object functions:

  • To access a function inside of an object in python, the notation similar to accessing a variable.

In [24]:
print(first_object.some_function())


This is a message inside the class.

Part 03 -- Constructor or initializer in python:

  • The init function is called a constructor, or initializer.
  • Construcor or initializer is automatically called when you create a new instance of a class.
  • init doesn't initialize a class, it initializes an instance of a class or an object.

A slightly more complex example of object oriented programing:

  • Uses a constructor for variables "name" and "type".
  • Uses a class variable, "kind".
  • Has an object function, "color".

In [25]:
class Dog:
  kind = 'I am a dog. According to scientists, I belong to a species called Canis lupus, under the sub-species familiaris.'
  
  def __init__(self, name =  None, type = None):
    self.name = name
    self.type = type

  def dog_color(self, color = None):
    if color and self.name and self.type != None:
      color = color.lower()
      if color == ('pink'):
        print ("Your dog " + str(self.name) + "'s " + color + " color is so cute ...")
      elif color == ('blue'):
        print ("Your dog " + str(self.name) + "'s " + color + " color makes it look like a smurf ...")
      else:
        print ("Your dog: " + str(self.name) + "'s " + "color is: " + color + ".")
    else:
      print ("Nothing to do here ...")
      
  def my_dog(self):
    if self.name and self.type != None:
      print ("Your dog " + str(self.name) + " is: " + str(self.type))
    else:
      print ("Nothing to do here ...")

In [26]:
my_dog = Dog()

In [27]:
my_dog.name = 'Puppy'

In [28]:
my_dog.type = 'Maltese'

In [29]:
print(my_dog)


<__main__.Dog object at 0x03D8FF10>

In [30]:
print(my_dog.name)


Puppy

In [31]:
print(my_dog.kind)


I am a dog. According to scientists, I belong to a species called Canis lupus, under the sub-species familiaris.

In [32]:
print(my_dog.type)


Maltese

In [33]:
my_dog = Dog(name = 'Puppy', type = 'Maltese')

In [34]:
my_dog.my_dog()


Your dog Puppy is: Maltese

An object can store any random variable:

  • An object can store any variables, not necessarily those sepcified in the class.

In [35]:
my_dog.hello_world = "Hello World"

In [36]:
my_dog.hello_world


Out[36]:
'Hello World'

Accessing an object function that also takes a keyword argument:


In [37]:
my_dog.dog_color(color = 'Pink')


Your dog Puppy's pink color is so cute ...

In [38]:
my_dog.dog_color(color='Blue')


Your dog Puppy's blue color makes it look like a smurf ...

Change the values of a variable in an object:


In [39]:
my_dog.name = "Cute Puppy"

In [40]:
my_dog.my_dog()


Your dog Cute Puppy is: Maltese

In [41]:
my_dog.dog_color(color='Black')


Your dog: Cute Puppy's color is: black.