[go: up one dir, main page]

0% found this document useful (0 votes)
26 views13 pages

OOPs IN PYTHON-NOTES-1

Object-Oriented Programming (OOP) is a programming paradigm that utilizes classes and objects to design software. A class serves as a blueprint for creating objects, which are instances containing state and behavior. In Python, classes are defined using the 'class' keyword, and the __init__() method initializes object properties upon instantiation.

Uploaded by

npranathi2006
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)
26 views13 pages

OOPs IN PYTHON-NOTES-1

Object-Oriented Programming (OOP) is a programming paradigm that utilizes classes and objects to design software. A class serves as a blueprint for creating objects, which are instances containing state and behavior. In Python, classes are defined using the 'class' keyword, and the __init__() method initializes object properties upon instantiation.

Uploaded by

npranathi2006
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/ 13

UNIT-5- Object-Oriented Programming in Python

What Is Object-Oriented Programming?


 Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.
 Object means a real-world entity such as a pen, chair, table, computer, watch, etc.

 Object: Any entity that has state and behaviour is known as an object.
 For Example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
 An Object can be defined as an instance of a class.
 An object contains an address and takes up some space in memory.
 Class: Collection of objects is called class. It is a logical entity.

1
 A class can also be defined as a blueprint from which you can create an individual object.
 Class doesn't consume any space.

Classes and Objects


 Let’s take an example. In the real world, you’ll often find many individual objects of all the same kind.
 There may be thousands of cars in existence, all of the same make and model.
 Each of these cars were built from the same set of blueprints and, therefore, contains the same components.
 In object-oriented terms, we say that each of these cars are objects of the class known as Car.
 A class is a blueprint from which individual objects are created.
 An object is a bundle of related state (variables) and behaviour (methods).
 Objects contain variables, which represents the state of information about the thing you are trying to model,
and the methods represent the behaviour or functionality that you want it to have.

Figure: Generic Object Diagram


2
Creating Classes in Python
 Related variables and methods are grouped together in classes.
 Class Definition Syntax:

 Classes are defined by using the class keyword, followed by the ClassName and a colon.
 Class definitions must be executed before they have any effect.
 In practice, the statements inside a class definition will usually be function definitions, but few other statements
are allowed.
 Methods are a special kind of function that is defined within a class.

3
The self
 Class methods must have an extra first parameter in the method definition.
 We do not give a value for this parameter when we call the method, Python provides it.

The __init__ method


 Built-in __init__() function.
 This __init__() method is also known as the constructor method.
 Constructors are used to initialize the object’s state.
 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.
 Like methods, a constructor also contains a collection of statements (i.e., instructions) that are executed at
the time of Object creation.
 It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you
want to do with your object.

Note: The __init__() function is called automatically every time the class is being used to create a new object.

4
Class Objects
 An Object is an instance of a Class.
 A class is like a blueprint while an instance is a copy of the class with actual values.
 Example: You can have many dogs to create many different instances, but without the class as a guide,
you would be lost, not knowing what information is required.

An object consists of:


 Identity: It gives a unique name to an object and enables one object to interact with other objects.

 State: It is represented by the attributes of an object. It also reflects the properties of an object.

 Behaviour: It is represented by the methods of an object. It also reflects the response of an object to other

objects.

5
Declaring Objects (Also called instantiating a class)
 When an object of a class is created, the class is said to be instantiated.
 All the instances(objects) share the attributes and the behaviour of the class.
 But the state/values of those attributes are unique for each object.
 A single class may have any number of instances(objects).
 Example:

6
Python Classes/Objects
 Python is an object-oriented programming language.

 Almost everything in Python is an object, with its properties and methods.

 A Class is like an object constructor, or a "blueprint" for creating objects .

Create a Class: To create a class, use the keyword class:

Create Object: Now we can use the class named MyClass to create objects:

7
8
9
10
Figure: Object diagram for Mobile class with data attributes and methods.

11
Create a class named Person, use the __init__() function to assign values for name and age:

12
13

You might also like