Classes and Objects in Python✌
![]() |
Python Classes |
- 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.
Defining a class
Like using def for defining a function, we can use class to define a class, according to python coding style rules the class name should start with an uppercase letter (not mandatory but important).
Syntax:
# Statement-1
.......
......
......
# Statement-N
Example:
name='sam'
Unlike functions, the class doesn't contain any arguments with it and the class also contains some special inbuild methods like every other object.
Objects
An object is an instance of a class. By using the created class we can create objects.
class Student:
name='sam'
s = Student()
print(s.name)
Constructors
After creating an object we have to create constructors in order to get variables from the object for the class. All classes have a function called __init__(), which is always executed when the class is being initiated.
For example, let us try a small student name program that stores the name and age of the given student.
Example:
def __init__(self,age,name):
self.age = age
self.name = name
stud1 = Student(23,'sam')
stud2 = Student(21,'jack')
print(stud1.name,stud2.name)
>> sam, jack
Here we can see that the constructor gets the value and assigns it to the class, which can be used through the class.
self- The purpose of using self is when we create an object it automatically stores the name at the argument place of the class by default ‘Student()’ so it is mandatory to mention it in the constructor, we can use any variables in the place of self but the word self is preferred by the programmers.
Creating a function inside a class
Basically, the constructor itself is a function other than that we can create functions inside the class and call it whenever needed but while calling inside a class the syntax should be self.function_name().
Example:
class Student:
def __init__(self,age,name):
self.age = age
self.name = name
def PrintName(self):
self.grade = 'First'
print('student name is ',self.name)
stud1 = Student(21,'tom')
stud1.PrintName()
Output:
>> tom
class built-in methods
As we discussed above class contains many special methods inside it, some of the most important of them are __doc__,__eq__ and __del__.
__doc__ → This is used to get all the comments placed inside the class.
Example:
''' this is a class '''
pass
Student.__doc__
__eq__ → This is used to return whether the object in the class are equal or not. you can ask that we can do this using == operator but it returns false when it comes to class. For example,
def __init__(self,name):
self.name = name
p1=Passenger('tom')
p2=Passenger('tom')
print(p1==p2)
If we use __eq__ operator and if we execute the same program it will return True.
Example:
def __init__(self,name):
self.name = name
def __eq__(self,other):
if isinstance(other,Passenger):
if self.name==other.name:
return True
return False
p1=Passenger(‘tom’)
p2=Passenger(‘tom’)
p1.__eq__(p2)
print(p1==p2)
Here isintance(object, class) is used to check whether the argument is an instance of the given class.
__del__ → This is used to delete an instance and we cannot perform any operations related to that instance after that. It acts like a destructor
Example:
def__init__(self,name,age):
self.name = name
self.age = age
def__del__(self):
print('Instance is deleted')
stud1=Student('tom',12)
del stud1
print(stud1.name)
we can also delete attributes using del function.
Comments
Post a Comment