Python Tutorial 36 --- "Creating our first Class(OOPS1)"
Python Tutorial 36 --- "Creating our first Class(OOPS1)"
Let's begin our Learning:
As procedure-oriented programming focuses on functions, object-oriented programming stresses on objects. An object is simply a collection of data and methods and a class is a blueprint for that object. We have already discussed the OOP in Python in the previous Classes & Objects(OOPS) tutorial. This tutorial is based on creating objects and classes.
Defining a Class in Python
Like function, definitions begin with the keyword def, class begins with a class keyword.
A class is a blueprint from which objects are created. Creating a new class creates a new type of object which allows the new instances of that type to be made. Each class instance has attributes attached so that it could maintain its state. Class instances can also have methods that are defined by its class for modifying its state.
Let us understand the concept of class through an example. Suppose we have to create a program that requires the data of all the individuals in a school. We will create three different classes, one for students, one for teaching staff, and one for accounting officers and others. The separation of the class is based on attributes because a teacher’s attributes are different from students, and both have different attributes from the members of account officers. Although many attributes are the same, such as name, age, address, etc. but the teacher also has an attribute salary that the student does not or an attribute, number of classes that the accounts officers do not possess. So, now we have an understanding of how and on what basis we form different classes.
Classes are not like function, so we do not have to use the keyword define to create a class; instead, we use the keyword Class along with the name of the class. Also, we do not call a class as a whole; instead, we use an object to access its different attributes. We can assign new values and can also overwrite the previous values with the help of an object. In short, an object gives us permission to access the whole class. We can access variables in a class, like:
Here we are setting a variable equal to abc. By doing this it's previous value will be overwritten.
Creating Object:
Creating an object of a class is rather easy and simple. Suppose we have a class named Student. We can create an object of it by these certain lines of code:
Here we have created two objects of class Student. We can access every item in student using these objects. There is no restriction on the number of objects a class may have, and also there is no limit to the number of classes a program may have.
An object consists of :
- The State, which is represented by attributes of an object which reflects the properties of an object.
- Methods of an object, which represents the behavior of the object and the response of an object with other objects.
- Identity, which gives a unique name to an object, so that one object can interact with other objects.
CODE---
# Creating Our First Class In Python
class Student:
pass
Saswata = Student()
Tanmoy = Student()
Ritam = Student()
Saswata.name = "Saswata"
Tanmoy.std = 12
Ritam.sec = "B"
Saswata.subs = ["Bengali", "English", "Maths", "Bios"]
print(Saswata.name, Saswata.subs,Tanmoy.std, Ritam.sec)
Comments
Post a Comment