0% found this document useful (0 votes)
32 views37 pages

Unit 7 Oop

Python Programming

Uploaded by

jayjoshi80208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views37 pages

Unit 7 Oop

Python Programming

Uploaded by

jayjoshi80208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit 7: OOP

Prepared By:
Tanvi Patel
Asst. Prof. (CSE)
Contents

◉ Classes and Object


◉ Defining a class in Python
◉ Creating a object in Python
◉ OOPS concept in Python
Classes and Objects

◉ Python is a multi-paradigm programming language. It supports different programming approaches.


◉ One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-
Oriented Programming (OOP).
◉ Python is a object oriented programming language. Unlike procedure oriented programming, where the main
emphasis is on functions, object oriented programming stresses on objects.
◉ Almost everything in Python is an object, with its properties and methods defined in class.
◉ A class is a blueprint which is followed by objects.
◉ Every class have multiple instance called objects.
◉ Object should follow the blueprint of class.
◉ Class is a logical structure with behaviour.
◉ We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows
etc. Based on these descriptions we build the house. House is the object.
◉ As many houses can be made from a house's blueprint, we can create many objects from a class. An object is also
called an instance of a class and the process of creating this object is called instantiation.
Classes and Objects (Cont.)
Classes and Objects (Cont.)

◉ Class is a prototype or a design of any object in the existence. It represents a set of


properties and behaviour of an object.
◉ Object is a physical existence of the class, it can be referred as an instance of a
class.
◉ A class contains:
◉ Variables ( which depicts certain properties of an object ) and
◉ Methods ( represents a set of behaviour of an object ).
Classes and Objects (Cont.)

◉ An object has two characteristics:


attributes or properties
behaviour
◉ Let's take an example:
◉ A parrot is can be an object,as it has the following properties:
name, age, color as attributes
singing, dancing as behaviour
◉ The concept of OOP in Python focuses on creating reusable code. This concept is
also known as DRY (Don't Repeat Yourself).
Classes and Objects (Cont.)

◉ Some points on Python class:


 Classes are created by keyword class.
 Attributes are the variables that belong to a class.
 Attributes are always public and can be accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
Defining a class in python

◉ Like function definitions begin with the def keyword in Python, class definitions
begin with a class keyword.
◉ The first string inside the class is called docstring and has a brief description
about the class. Although not mandatory, this is highly recommended.
◉ Here is a simple class definition.
Defining a class in python (Cont.)
Defining a class in python (Cont.)

◉ A class creates a new local namespace where all its attributes are defined.
Attributes may be data or functions.
◉ There are also special attributes in it that begins with double underscores __. For
example, __doc__ gives us the docstring of that class.
◉ As soon as we define a class, a new class object is created with the same name.
This class object allows us to access the different attributes as well as to instantiate
new objects of that class.
Creating a object in python

◉ We saw that the class object could be used to access different attributes.
◉ It can also be used to create new object instances of that class. The procedure to
create an object is similar to a function call.

◉ This will create a new object instance named harry. We can access the attributes of
objects using the object name prefix.
◉ Attributes may be data or method. Methods of an object are corresponding functions
of that class.
◉ This means to say, since Person.greet is a function object (attribute of
class), Person.greet will be a method object.
Creating a object in python (Cont.)
Creating a object in python (Cont.)

◉ You may have noticed the self parameter in function definition inside the class but
we called the method simply as harry.greet() without any arguments. It still worked.
◉ This is because, whenever an object calls its method, the object itself is passed as
the first argument. So, harry.greet() translates into Person.greet(harry).
◉ In general, calling a method with a list of n arguments is equivalent to calling the
corresponding function with an argument list that is created by inserting the
method's object before the first argument.
◉ For these reasons, the first argument of the function in class must be the object
itself. This is conventionally called self.
The self Parameter

◉ The self parameter is a reference to the current instance of the class, and is
used to access variables that belongs to the class.
◉ It does not have to be named self, you can call it whatever you like, but it has to be
the first parameter of any function in the class.
The __init__() Function

◉ The examples above are classes and objects in their simplest form, and are not
really useful in real life applications.
◉ To understand the meaning of classes we have to understand the built-in __init__()
function.
◉ All classes have a function called __init__(), which is always executed when the
class is being initiated.
◉ Use the __init__() function to assign values to object properties, or other
operations that are necessary to do when the object is being created:
The __init__() Function (Cont.)
Object Methods

◉ Objects can also contain methods. Methods in objects are functions that belong to
the object.
◉ Let us create a method in the Person class:
Object Methods (Cont.)
Modify the Object Properties

◉ You can modify the properties on object like below shown:


Delete Object Properties

◉ You can delete the properties on objects by using the del keyword.
Delete Objects

◉ You can delete objects by using the del keyword.


The pass statement

◉ Class definition cannot be empty, but for some reason have a class definition with
no content, put in the pass statement to avoid getting an error.
Variables in OOP

◉ Variables in a class are related to an object, to a class or to a method.


◉ There are two different types of variables in OOPs in python.
◉ Instance variable ( Object level variable )
◉ Static variable ( Class level variable )
Variables in OOP

Class

Class Instance Data


variable variable member

A class
variable or
A variable Instance
instance
that is shared variable
variable that
by all unique to
holds data
instances of a each associated
class. instance. with a class
and its objects
Variables in OOP

◉ Class Variables — Declared inside the class definition (but outside any of the instance
methods). They are not tied to any particular object of the class, hence shared across all the
objects of the class. Modifying a class variable affects all objects instance at the same time.
◉ Instance Variable — Declared inside the constructor method of class (the __init__ method).
They are tied to the particular object instance of the class, hence the contents of an instance
variable are completely independent from one object instance to the other.
◉ Let’s start with a short and easy-to digest example:
class Car:
wheels = 4 # <- Class variable
def __init__(self, name):
self.name = name # <- Instance variable
Variables in OOP

◉ In above example Car class is defined. Each instance of it will have class variable wheels along
with the instance variable name.
◉ >>> jag = Car('jaguar')
>>> fer = Car('ferrari')
◉ >>> jag.name, fer.name
('jaguar', 'ferrari')
◉ >>> jag.wheels, fer.wheels
(4, 4)
◉ >>> Car.wheels
4
Variables in OOP (Summary)

◉ When we create multiple objects ( let’s say : e1, e2…e10 ) of Employee, name,
company and address is different for each employee. Hence, we can say that Instance
variables ( object level variables ) are variables where value varies from object to object.
We can declare instance variables:
◉ Inside a constructor using self (shown above ).
◉ Inside an Instance method.
◉ Outside of the class using object reference.
◉ Instance variables are created at the time of object creation and will be destroyed when it goes
for Garbage collection.
◉ For every object separate copy of instance variables will be created.
◉ We can use __dict__ to know the instance variables and their corresponding values of an
object. It returns values in a dictionary format.
Example
Example

Output:
Example
OOPS CONCEPT IN PYTHON

Inheritance Encapsulation

Abstraction Polymorphism
OOPS CONCEPT IN PYTHON (Cont.)

 A class can inherit attributes and behaviour methods


from another class, called superclass.
Inheritance
 A class which inherits from a superclass is called
subclass, also called their class or child class.
Abstraction

◉ Here we have different types of trucks and cars.


They have different color, shape, engine which
makes them distinct.
◉ But they have some common properties and
behavior among them i.e., they all have tires,
engine, steering, gear etc. and are used for
travelling and can be operated in a common way.
◉ Vehicle can be the general idea of a car or truck.
It retains only the information on general vehicle
attributes and behavior eliminating the other
characteristics of a particular car or a truck.
◉ Abstraction means hiding the implementation
details and only show the essential details.
Encapsulation

◉ Wrapping up data and function together into a single unit is called encapsulation.
◉ This feature keeps the data safe from outside interference and misuse. This lead to
the concept of Data Hiding.
Polymorphism

◉ Polymorphism is a Greek term which means ability to take more than one form.
◉ Example1: If you learned driving one car, you’ll be able to drive any car, it doesn’t
depend on car brand or inner implementation. It has same interface.
◉ Example 2: Water
Conclusion

◉ Object-Oriented Programming makes the program easy to understand as well as


efficient.
◉ Since the class is sharable, the code can be reused.
◉ Data is safe and secure with data abstraction.
◉ Polymorphism allows the same interface for different objects, so programmers can
write efficient code.
◉ Follows bottom up approach
◉ This language also emphasizes reusability through inheritance and the capacity to
spread current implementations without having to change a great deal of code by
using polymorphism.
Thank You

You might also like