Pyhon OOPS
Pyhon OOPS
Programming paradigms
• Focus on data
• Solving real world problem
Object Oriented • Example: C++,Java, Python
Overview of OOP Terminology
Class: A user-defined prototype for an object that defines a set
of attributes that characterize any object of the class. The
attributes are data members
It’s a template to create object.
A way to create User defined data type to be able to
represent real world object and hence solve real word problem.
Class variable: A variable that is shared by all instances of a
class. Class variables are defined within a class but outside any
of the class's methods. Class variables are not used as
frequently as instance variables are.
Inheritance: The transfer of the characteristics of a class to
other class. Facilitates Code Reusability and feature
enhancement.
Data member: A class variable or instance variable that holds
data associated with a class and its objects.Classes that are
derived from it.
Overview of OOP Terminology (Contd..)
Instance: An individual object of a certain class. An object obj
that belongs to a class Circle, for example, is an instance of the
class Circle.
Instance variable: A variable that is defined inside a method
and belongs only to the current instance of a class.
Instantiation: The creation of an instance of a class.
Method : A special kind of function that is defined in a class
definition.
Object: A unique instance of a data structure that's defined by
its class. An object comprises both data members class variables
and instance variables and methods.
Encapsulation : Dividing the code into a public interface, and
a private implementation of that interface
Object Oriented Programming
in Python:
Defining Classes
It’s all objects…
Everything in Python is really an object.
• We’ve seen hints of this already…
“hello”.upper()
list3.append(‘a’)
dict2.keys()
• These look like Java or C++ method calls.
• New object classes can easily be defined in
addition to these built-in data-types.
In fact, programming in Python is typically
done in an object oriented fashion.
Defining a Class
class student:
“““A class representing a
student ”””
def __init__(self,n,a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Creating and Deleting
Instances
Instantiating Objects
There is no “new” keyword as in Java.
Just use the class name with ( ) notation and
assign the result to a variable
__init__ serves as a constructor for the
class. Usually does some initialization work
The arguments passed to the class name are
given to its __init__() method
So, the __init__ method for student is passed
“Bob” and 21 and the new class instance is
bound to b:
b = student(“Bob”, 21)
Constructor: __init__
An __init__ method can take any number of
arguments.
Like other functions or methods, the
arguments can be defined with default values,
making them optional to the caller.
class student:
“““A class representing a student
”””
def __init__(self,n,a):
self.full_name = n
self.age = a
def get_age(self):
return self.age
Traditional Syntax for Access