Python Opps

Solving a problem by creating objects is one of the most popular approaches in programming. This is called object-oriented programming.

This concept focuses on using reusable code. (Implements DRY principle)

Opps Terminology

  • class: Classes are a user-defined data type that is used to encapsulate data and associated functions together. It also helps in binding data together into a single unit.
  • Data Member: A variable or memory location name that holds value to does a specific task within a program.
  • Member Function: They are the functions, usually a block of a code snippet that is written to re-use it.
  • Instance variable: A variable that is defined inside a method of a class.
  • Function Overloading: This technique is used to assign multiple tasks to a single function, and the tasks are performed based on the number of arguments or the type of argument the function has.
  • Operator Overloading: It is the technique of assigning multiple functions/tasks to a particular operator.
  • Inheritance: It is the process of acquiring the properties of one class to another, i.e., one class can acquire the properties of another.
  • Instantiation: It is the technique of creating an instance of a class.

Class

A class is a blueprint for creating objects.

The syntax of a class looks like this:

Class Employee:        [classname is written in PascalCase]

        #methods & variables

Object

An object is an instantiation of a class. When class is defined, a template(info) is defined. Memory is allocated only after object instantiation.

Objects of a given class can invoke the methods available to it without revealing the implementation details to the user.     #Abstraction & Encapsulation!

Modelling a problem in OOPs

We identify the following in our problem

Noun -> Class -> Employee

Adjective -> Attributes -> name,age,salary

Verbs -> Methods -> getSalary(), increment()

Class Attributes

An attribute that belongs to the class rather than a particular object.

Example:

Class Employee:

        company = “Google”     #Specific to each class

Sandeep = Employee()   #Object instantiation

Sandeep.company

Employee.company = “YouTube”   #changing class attribute

Instance Attributes

An attribute that belongs to the Instance (object)

Assuming the class from the previous example:

Sandeep.name = “Sandeep”

Sandeep.salary = “30K” #Adding instance attributes

Note: Instance attributes take preference over class attributes during assignment and retrieval.

Sandeep.attribute1  :

  1. Is attribute1 present in the object?
  2. Is attribute1 present in class?

‘self’ parameter

self refers to the instance of the class.

It is automatically passed with a function call from an object.

Sandeep.getSalary()

here, self is Sandeep, and the above line of code is equivalent to Employee.getSalary(Sandeep)

This function getsalary is defined as:

class Employee:

        company = “Google”

        def getSalary(self):

               print(“Salary is not there”)

Static method

Sometimes we need a function that doesn’t use the self-parameter. We can define a static method like this:

@staticmethod  #decorator to mark greet as a static method

def greet():

        print(“Hello user”)

__init__() constructor

__init__() is a special method which runs as soon as the object is created.

__init__() method is also known as constructor.

It takes self-argument and can also take further arguments.

For Example:

class Employee:

        def __init__(self,name):

               self.name = name

        def getSalary(self):

               #Some code…

Sandeep = Employee(“Sandeep”)  #Object can be instantiated using constructor like this!

Exercise On Opps

  1. Create a class programmer for storing information of a few programmers working at Microsoft.
  2. Write a class calculator capable of finding square, cube, and the square root of a number.
  3. Create a class with a class attribute a; create an object from it and set a directly using object.a=0 Does this change the class attribute?
  4. Add a static method in problem 2 to greet the user with hello.
  5. Write a class Train which has methods to book a ticket, get status(no of seats), and get fare information of trains running under Indian Railways.
  6. Can you change the self parameter inside a class to something else (say ‘Sandeep’)? Try changing self to ‘slf’ or ‘Sandeep’ and see the effects.
Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *