Python Object and Class
Python is an object-oriented programming language. So, its main focus is on objects, unlike procedure-oriented programming languages which mainly focuses on functions.
In an object-oriented programming language, the object is simply a collection of data (variables) and methods (functions) that act on those data.
Python Class
A class is a blueprint for the object. Let's understand it by an example:
Suppose a class is a prototype of a building. A building contains all the details about the floor, doors, windows, etc. we can make other buildings (as many as we want) based on these details. So the building is a class and we can create many objects from a class.
An object is also called an instance of a class and the process of creating this object is known as instantiation.
Python classes contain all the standard features of Object Oriented Programming. A python class is a mixture of the class mechanism of C++ and Modula-3.
Python Object Class Example
class Student:
def __init__(self, rollno, name):
self.rollno = rollno
self.name = name
def displayStudent(self):
print "rollno : ", self.rollno, ", name: ", self.name
emp1 = Student(121, "neha")
emp2 = Student(122, "siwa")
emp1.displayStudent()
emp2.displayStudent()
def __init__(self, rollno, name):
self.rollno = rollno
self.name = name
def displayStudent(self):
print "rollno : ", self.rollno, ", name: ", self.name
emp1 = Student(121, "neha")
emp2 = Student(122, "siwa")
emp1.displayStudent()
emp2.displayStudent()
0 comments:
Post a Comment