- In python every thing is an object. To create objects we required some model or plan or blueprint, which is nothing but class.
Class
- We can write a class to represent properties (attributes) and actions (behavior) of object.
- Properties can be represented by variables.
- Actions can be represented by methods.
- We can define a class by using class keyword.
Syntax
Python
class className:
''' documenttation string '''
# variables
# methods
- Documentation string represents description of the class. Within the class doc string is always optional. We can get doc string by using the following 3 ways.
- print(classname.__doc__)
- print(objectreference.__doc__)
- help(classname)
Example
Python
class Student:
'''This is student class with required data'''
student = Student()
print(Student.__doc__)
print(student.__doc__)
Output
PowerShell
This is student class with required data
This is student class with required data
- Within the python class we can represent data by using variables. There are 3 types of variables are allowed.
- Instance variables (Object level variables)
- Static variables (Class level variables)
- Local variables (Method level variables)
- Within the python class, we can represent operations by using methods. The following are various types of allowed methods:
- Instance methods
- Class methods
- Static methods
Example
Python
class Student:
'''Developed by ajay for python demo'''
def __init__(self):
self.name='amit'
self.age=25
self.marks=80
def talk(self):
print("Hello I am :",self.name)
print("My Age is:",self.age)
print("My Marks are:",self.marks)
student = Student()
student.talk()
Output
PowerShell
Hello I am : amit
My Age is: 25
My Marks are: 80
Object
- Physical existence of a class is nothing but object. We can create any number of objects for a class.
Syntax
Python
referencevariable = classname()
- The variable which can we used to refer object is called reference variable. By using reference variable, we can access properties and methods of object.
Example
Python
class Student:
'''Developed by amit for python demo'''
def __init__(self):
self.name='ajay'
self.age=28
self.marks=90
def talk(self):
print("Hello I am :",self.name)
print("My Age is:",self.age)
print("My Marks are:",self.marks)
# Object decleration
student = Student()
student.talk()
Output
PowerShell
Hello I am : ajay
My Age is: 28
My Marks are: 90
Example
Write a python program to create a student class and creates an object to it. Call the method info() to display student details?
Python
class Student:
def __init__(self,name,rollno,marks):
self.name=name
self.rollno=rollno
self.marks=marks
def info(self):
print("Hello My Name is:",self.name)
print("My Rollno is:",self.rollno)
print("My Marks are:",self.marks)
student = Student("amit",50,80)
student.info()
Output
PowerShell
Hello My Name is: amit
My Rollno is: 50
My Marks are: 80
Self variable
- self is the default variable which is always pointing to current object.
- By using self we can access instance variables and instance methods of object.
Note
- self should be first parameter inside constructor.
- self should be first parameter inside instance methods.
Constructor
- Constructor is a special method in python.
- The name of the constructor should be __init__(self).
- Constructor will be executed automatically at the time of object creation.
- The main purpose of constructor is to declare and initialize instance variables.
- Per object constructor will be exeucted only once.
- Constructor can take atleast one argument self.
- Constructor is optional and if we are not providing any constructor then python will provide default constructor.
Example
Python
class Student:
def __init__(self,name,rollno,marks):
self.name=name
self.rollno=rollno
self.marks=marks
print('Constructor Executed')
student = Student("ajay", 5, 99)
Output
PowerShell
Constructor Executed
Example
Demonstrated constructor will execute only once per object
Python
class Test:
def __init__(self):
print("Constructor exeuction...")
def m1(self):
print("Method execution...")
test1=Test()
test2=Test()
test3=Test()
test1.m1()
Output
PowerShell
Constructor exeuction...
Constructor exeuction...
Constructor exeuction...
Method execution...
Example
Python
class Student:
''''' This is student class with required data'''
def __init__(self,name,rollno,marks):
self.name=name
self.rollno=rollno
self.marks=marks
def display(self):
print("Student Name:{}\nRollno:{} \nMarks:{}".format(self.name,self.rollno,self.marks))
student1=Student("ajay",1,80)
student1.display()
student2=Student("amit",2,100)
student2.display()
Output
PowerShell
Student Name:ajay
Rollno:1
Marks:80
Student Name:amit
Rollno:2
Marks:100
Methods Vs Constructors
There are mainly 4 difference between methods and constructors.
Method | Constructor |
Name of method can be any name. | Constructor name should be always __init__. |
Method will be executes if we call that methods. | Constructor will be executed automatically at the time of object creation. |
Per object, method can be called any number of times. | Per object, Constructor will be executed only once. |
Inside method we can write business logic. | Inside constructor we have to declare and initialize instance variables. |
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!