0% found this document useful (0 votes)
8 views

Unit 2 Python Ppt Prabha(1)

This document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It details the role of constructors in initializing objects, the types of constructors available, and the use of getters and setters for data encapsulation. Additionally, it highlights the importance of object properties and the ability to modify and delete objects in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 2 Python Ppt Prabha(1)

This document provides an overview of Object-Oriented Programming (OOP) in Python, explaining key concepts such as classes, objects, attributes, and methods. It details the role of constructors in initializing objects, the types of constructors available, and the use of getters and setters for data encapsulation. Additionally, it highlights the importance of object properties and the ability to modify and delete objects in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

UNIT 2

OBJECT ORIENTED PROGRAMMING

PREPARED BY
DR. SHANMUGA PRABHA P
ASSISTANT PROFESSOR
DEPT. OF CSE
Python is an object-oriented programming language. This means that almost all the code is
implemented using a special construct called classes. A class is a code template for creating
objects.

What is a Class and Objects in Python?

•Class: The class is a user-defined data structure that binds the data members and methods
into a single unit. Class is a blueprint or code template for object creation. Using a class,
you can create as many objects as you want.

•Object: An object is an instance of a class. It is a collection of attributes (variables) and


methods. We use the object of a class to perform actions.

Objects have two characteristics: They have states and behaviors (object has attributes
and methods attached to it) Attributes represent its state, and methods represent its behavior.
Using its methods, we can modify its state.

In short, Every object has the following property.


•Identity: Every object must be uniquely identified.
•State: An object has an attribute that represents a state of an object, and it also reflects the
property of an object.
•Behavior: An object has methods that represent its behavior.
Python is an Object-Oriented Programming language, so everything in Python is treated as
an object. An object is a real-life entity. It is the collection of various data and functions that
operate on those data.
For example, If we design a class based on the states and behaviors of a Person, then
States can be represented as instance variables and behaviors as class methods.
Create a Class in Python

Class is defined using keyword: class


Create Object of a Class
An object is essential to work with the class attributes. The object is
created using the class name. When we create an object of the class, it
is called instantiation. The object is also called the instance of a class.
A constructor is a special method used to create and initialize an object
of a class. This method is defined in the class.
In Python, Object creation is divided into two parts in Object
Creation and Object initialization
•Internally, the __new__ is the method that creates the object
•And, using the __init__() method we can implement constructor to
initialize the object.
Class Attributes
When we design a class, we use instance variables and class
variables.
In Class, attributes can be defined into two parts:
•Instance variables: The instance variables are attributes
attached to an instance of a class. We define instance
variables in the constructor ( the __init__() method of a
class).
•Class Variables: A class variable is a variable that is
declared inside of class, but outside of any instance method
or __init__() method.
Objects do not share instance attributes. Instead, every object has its
copy of the instance attribute and is unique to each object.
All instances of a class share the class variables. However, unlike
instance variables, the value of a class variable is not varied from object
to object.
Only one copy of the static variable will be created and shared between
all objects of the class.
Accessing properties and assigning values
•An instance attribute can be accessed or modified by using the dot
notation: instance_name.attribute_name.
•A class variable is accessed or modified using the class name
Class Methods
In Object-oriented programming, Inside a Class, we can define the following three
types of methods.
•Instance method: Used to access or modify the object state. If we use
instance variables inside a method, such methods are called instance methods.
•Class method: Used to access or modify the class state. In method implementation,
if we use only class variables, then such type of methods we should declare as a class
method.
•Static method: It is a general utility method that performs a task in isolation. Inside
this method, we don’t use instance or class variable because this static method
doesn’t have access to the class attributes.
Object Properties
Every object has properties with it. In other words, we can say that object property is an
association between name and value.

For example, a car is an object, and its properties are car color, sunroof, price,
manufacture, model, engine, and so on. Here, color is the name and red is the value.
Object properties are nothing but instance variables.
Modify Object Properties
Every object has properties associated with them. We can set or modify the
object’s properties after object initialization by calling the property directly
using the dot operator.
Obj.PROPERTY = value
Delete Objects
n Python, we can also delete the object by using a del keyword.
I

An object can be anything like, class object, list, tuple, set, etc.
Constructors in Python

Constructor is a special method used to create and initialize an object of a class. On the
other hand, a destructor is used to destroy the object.

This method is defined in the class.


•The constructor is executed automatically at the time of object creation.
•The primary use of a constructor is to declare and initialize data member/ instance variables
of a class. The constructor contains a collection of statements (i.e., instructions) that
executes at the time of object creation to initialize the attributes of an object.
Where,
•def: The keyword is used to define function.
•__init__() Method: It is a reserved method. This method
gets called as soon as an object of a class is instantiated.
•self: The first argument self refers to the current object. It
binds the instance to the __init__() method. It’s usually
named self to follow the naming convention.
Note: The __init__() method arguments are optional. We
can define a constructor with any number of arguments.
Note:
•For every object, the
constructor will be executed
only once. For example, if we
create four objects, the
constructor is called four
times.

•In Python, every class has a


constructor, but it’s not
required to define it
explicitly. Defining
constructors in class is
optional.

•Python will provide a default


constructor if no constructor
is defined.
Types of Constructors
In Python, we have the following three types
of constructors.
•Default Constructor
•Non-parametrized constructor
•Parameterized constructor
Constructor Chaining
• Constructors are used for instantiating an object. The task of the
constructor is to assign value to data members when an object of
the class is created.
• Constructor chaining is the process of calling one constructor from
another constructor.
• Constructor chaining is useful when you want to invoke multiple
constructors, one after another, by initializing only one instance.
• In Python, constructor chaining is convenient when we are
dealing with inheritance.

• When an instance of a child class is initialized, the constructors of


all the parent classes are first invoked and then, in the end, the
constructor of the child class is invoked.
• Using the super() method we can invoke the parent class
constructor from a child class.
Constructor Return Value
In Python, the constructor does not return any value. Therefore, while declaring a
constructor, we don’t have anything like return type. Instead, a constructor is implicitly
called at the time of object instantiation. Thus, it has the sole purpose of initializing the
instance variables.
SUMMARY

•A constructor is a unique method used to initialize an object of the class.


•Python will provide a default constructor if no constructor is defined.
•Constructor is not a method and doesn’t return anything. it returns None
•In Python, we have three types of constructor default, Non-parametrized, and
parameterized constructor.
•Using self, we can access the instance variable and instance method of the
object. The first argument self refers to the current object.
•Constructor overloading is not possible in Python.
•If the parent class doesn’t have a default constructor, then the compiler would
not insert a default constructor in the child class.
•A child class constructor can also invoke the parent class constructor using
the super() method.
Getter and Setter in Python
A class can have one more variables (sometimes called properties). When you create objects each of those
objects have unique values for those variables.
Class variables need not be set directly: they can be set using class methods. This is the object orientated way
and helps you avoid mistakes.
In Python, getters and setters are not the same as those in other
object-oriented programming languages. Basically, the main
purpose of using getters and setters in object-oriented programs
is to ensure data encapsulation. Private variables in python are
not actually hidden fields like in other object oriented languages.
Getters and Setters in python are often used when:
•We use getters & setters to add validation logic around getting
and setting a value.
•To avoid direct access of a class field i.e. private variables cannot
be accessed directly or modified by external user.
Using normal function to achieve getters and setters
behaviour
To achieve getters & setters property, if we define
normal get() and set() methods it will not reflect any special
implementation. For Example
In above code
functions get_age() and set_age() acts as
normal functions and doesn’t play any impact
as getters and setters, to achieve such
functionality Python has a special
function
Using property().
property() function to achieve
getters and setters behaviour
In Python property()is a built-in function that
creates and returns a property object. A property
object has three methods, getter(), setter(), and
delete(). property() function in Python has four
arguments property(fget, fset, fdel, doc), fget is
a function for retrieving an attribute
value. fset is a function for setting an attribute
value. fdel is a function for deleting an attribute
value. doc creates a docstring for attribute. A
property object has three
methods, getter(), setter(), and delete() to
specify fget, fset and fdel individually.
In above code there is only one
print statement at line #25 but
output consists of three lines due
to setter method set_age() called
in line #23 and getter
method get_age() called in line
#25. Hence age is a property
object that helps to keep the
access of private variable safe.

You might also like