Unit 7 Oop
Unit 7 Oop
Prepared By:
Tanvi Patel
Asst. Prof. (CSE)
Contents
◉ 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 delete the properties on objects by using the del keyword.
Delete Objects
◉ 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
Class
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.)
◉ 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