Python 12
Python 12
Python 12
Python
Programming
Dr. Emmanuel S. Pilli
Associate Professor, CSE
Programming Paradigms
Python supports three types of Programming Paradigms
Structured Programming
Functional Programming
Object Oriented Programming
Classes and Objects
Object is a specific instance of a Class
Class describes two things
Form an object created from it will take
Functionality it will have
Form is often termed as properties (attributes) and
Functionality is termed as methods
Class bundles data (properties) and functionality together
Classes and Objects
Properties will have values (data members) and methods
(member functions) can access or manipulate the values
Class is generic in nature and Object is specific in nature
Multiple Objects can be created from a single class
Every type is a class – int, float, complex, bool, str, list,
tuple, set, dict are all classes
Classes and Objects
Class has a name but Objects are nameless. The names by
which objects are referred are addresses of memory
where the object is stored.
Specific data in an object is called instance data /
properties / attributes / state of the object
Methods of an object are called instance methods or
member functions
User defined Classes
Python permits users to create their own classes and
then create objects from them
The class statement creates a new class definition.
The name of the class immediately follows the keyword
class followed by a colon
class ClassName:
The nameless objects are created by the statement
objectName = ClassName( )
The address of the object is stored in objectName
Accessing Attributes
Object contains instance data and instance methods
Objects method can be called using
objectName.method()
Whenever we call an instance method using an object,
address of the object gets passed to the method
implicitly, and get is collected in a variable called self
Using the address of the object present in self the
object’s instance data is selected. Any other variable
name can also be used in place of self
Object Initialization
There are two ways
Using methods or member functions (assign as in
example shown in Notebook). Data remains protected
from manipulation from outside the class
Using special method __init( )__ (constructor
method). This guarantees initialization as __init( )__
is always called when an object is created.
__init__( )
Whenever an object is created, space so allocated for it in
memory and __init__( ) is called and addresses is passed
to __init__( )
__init__( ) does not return any value
If __init__( ) is not defined, Python inserts a default
method in our class
__init__( ) is only called once in the entire lifetime of an
object
A class may have a __init__( ) and a member function to
modify an initialized object
__del__ ( )
__del__( ) method gets called automatically when an
object goes out of scope
__del__( ) method should be used in clean up
activity
__del__( ) method is similar to destructor functions
Class Variables and Methods
If a variable is to be shared among all objects of a class, a class
variable or class attribute is declared.
A class variable is declared without using self
Class variables are not part of the objects of a class
Class variables are accessed using
ClassName.variableName
Class methods do not receive a self argument
Class methods are accessed using
ClassName.methodName( )
Class variables are used to count how many objects are created