Introduction
Python is an Object Oriented Programming Language(OOPs) and everything coded in Python is stored as an object. It may be the variables you declare, the functions you define or inbuilt functions, data types, etc. That’s why Python is an Object oriented programming language.
There are 7 features of OOPs that have been explained in a separate blog OOPs Concepts in Python. You can read by clicking in the given link.
In this blog we will learn in detail about Python Objects and Classes. Before goining through the concepts of Python Objects, let’s learn what is the meaning of Classes in Python and How you can implement with the help of python coding.
What is a Class in Python?
In Python class is like a blueprint to create an object. class is a python keyword. It bundles the features and functionalities of a class. Features data are stored in variables and for functionalities we define different functions. Let’s learn with an example of “Student” Class. Here class name is “Student”. What are the fetures of student class? His/Her Name, age, class, heigt, weight, address, etc are some features of Student class. As a coder we will create variables for each feature. Now What are the functions of class Student? Reading and writing are some basic function of any student. Then we will define a function with the help of programming language.
So, In this example you can see, we have defined some common features and functions of any student. If once we define the common features and functions of Student in a class then later on we can apply this class to store data of any number of students.
Syntax for Creating Class:
class Student:
name='Ray'
age=12
Class=10
def student_func(self):
print("I am a",self.name)
print("My age is",self.age)
print("I read in Class",self.class)
obj1 = Student()
obj1.student_func()
What is the need of class?
Suppose you have to collect different data from a 500 students in a school. Will you write a separate variables and functions for each student? Ans is no. You will define a class with require attribute and functions you want from student. Then you will create a separate object for each student. Now we will learn about Objects in depth and how you can create a new object with python. Let’s dive deeper.
Objects in Python With Examples
Objects is an instance of a class. When an object of a class created it means class is instantiated. We can create any number of objects from a class. Each object contains unique data or information.
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def func(self):
print("my name is",self.name)
print("my age is", self.age)
obj1 = Student("ray",12)
obj2 = Student("krish",10)
obj1.func()
obj2.func()
output my name is ray my age is 12 my name is krish my age is 10
In this example I have created an object Student. name and age is the attribute of Student. Then create a method, name is func(). After defining method i have created two instance of Student obj1 and obj2 with different student values. Then call function. Below you can see in output. Copy the code and practice same in any python editor.
self parameter
self parameter is a special parameter. It is used to call local variables or local functions defined in the same class
__init__() method
init is constructor in python. Whenever any class gets executed, first of all constructor is called and the parameters passes in constructor is first executed. Constructor values are default values. Constructor can be parameterised or non parameterised. We will talk about in more detail about constructors in next blog.
Objects and classes brings modularity in object oriented programming. We write codes in small – small blocks.