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

Python Recap - 4 OOP

Uploaded by

vashista vindla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Python Recap - 4 OOP

Uploaded by

vashista vindla
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Technology: Python

FDP- Day 3–Part 1

Copyright © 2019 TATA Consultancy Services Limited TCS Internal


1
CONTENT

Python – Classes and Objects


Python – Self keyword, init method
Python – Class and object variables

2
TCS Internal
Python Objects

An object is an entity (tangible or intangible) that has well


defined structure and behavior.

Contents
In OOP, a program is seen as comprising of a collection of
objects, that act on each other.

Each object has a distinct role or responsibility.

3
Python Classes

A class is a blueprint of an object.

This defines a set of attributes that characterizes any

Contents
object that is instantiated from this class.

An object is a realized version of a class. It can also be


defined as an abstract datatype.

4
Python - Attributes and methods

 Objects can store data using ordinary variables that belong to the object.

 Variables that belong to an object or class are referred to as fields.

 Objects can also have functionality by using functions that belong to a


class. Such functions are called methods of the class.

Contents
 Collectively, the fields and methods can be referred to as the attributes
of that class.

 Fields are of two types - they can belong to each instance/object of the
class or they can belong to the class itself.

 They are called instance variables and class variables respectively.

5
Basic Syntax

Example

6
The self keyword
 The self keyword represents the instance of a class
 By using the "self" keyword we can access the attributes and methods of
the class in python.
 Whenever an instance method is declared, the first parameter passed to
the method is the self keyword. This represents the object which calls the
method.
 Self is a convention and not a real python keyword – any other word can
be used other that self.

7
The __init__() method
 The init method is a reserved method in python which is used to
initialize an object
 When an object of a class is created/instantiated, the init method is
invoked automatically.
 It is almost similar to a constructor as per OOP concepts

Filename : person.py

Output

8
Python – Class & object variable
As discussed earlier, there are two types of data fields- class variables and object
variables
 Class variables are shared - they can be accessed by all instances of that
class. There is only one copy of the class variable and when any one object
makes a change to a class variable, that change will be seen by all the other
instances.

 Object variables are owned by each individual object/instance of the class. In


this case, each object has its own copy of the field i.e. they are not shared and
are not related in any way to the field by the same name in a different
instance.

 Let us look at an example which will make it a little easy to understand.

In this example, a person class is declared which has a class variable as


count and a class method is declared to get the count of employees.

There are two other functionalities, one to remove and employee and
another to receive a greeting from any employee. Both these methods are
object variables i.e. they will be called by an instance of that class.
9
Example
The implementation :

10
Example – Contd..
Contd ..

11
Output

12
13

You might also like