Python Language Tutorial: Class Object Oriented Language - Part Seven

Today, I will take a new part of Python for my tutorial series. In this part you will learn about Object Oriented in Python.

I already told you the following:

Python has been an Object oriented Programming language and also supports all concept of OOPS.

Python has many OOPS terminologies:

  • Class
  • Inheritance
  • Method
  • Object
  • Instance
  • Function Overloading

These are the basic OOPS concepts and Python supports all concepts.

Create Class:

  1. Firstly open python terminal and in Command Prompt of Windows and write code as below:

    class Student:

    o def __init__(self,name,age,grade):

    • self.name=name
    • self.age=age
    • self.grade = grade

      code

    In above image __init__ is is default function of python.It also work as constructor of parameter in python. We can give many parameter to this function. Self parameter work as object to show all method and variable.

  2. Now make Object of class and insert value of Student like below:

    Student1 = Student(“Neeraj”,12,”A”)

    code

    Class Attributes:

    These Class attributes built-in attributes that can use for specific task.

    • __dict__: Dictionary containing the class's namespace.
    • __doc__: Class documentation string or none, if undefined.
    • __name__: Class name.
    • __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode.
    • __dict__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.

  3. Now we example of all the above attributes.

    • print(Student.__doc__)
    • print(Student.__name__)
    • print(Student.__module__)
    • print(Student.__bases__)
    • print(Student.__dict__)

      code

    These define the Class Concept according to their functionality.

  4. Creation and Deletion of Object is easy in python as below code:

    • Student1 = Student("A",1,"A") // create student1 object
    • Student2 = Student("B",2,"B") // “”
    • del Student1 // delete the object student1
    • del Student2 //””

      code

    Functions in Classes:

    class Student:

    • def __init__(self,name,age,grade):

      a. self.name=name
      b. self.age=age
      c. self.grade = grade

    • def displayStudent(self):

      a. return ("Student name is "+self.name+" and age is "+str(self.age))

    • student1=Student("BOB",21,"A")
    • student1.displayStudent()

    Output: 'Student name is BOB and age is 21'

    Class Method Built-In:

      • getattr(obj, name[, default]) : to access the attribute of object.
      • hasattr(obj,name) : to check if an attribute exists or not.
      • setattr(obj,name,value) : to set an attribute. If attribute does not exist, then it would be created.
      • delattr(obj, name) : to delete an attribute

  5. Let's take an example of these Methods

    class Student:

    • def __init__(self,name,age

      • self.name=name
      • self.age=age
      • self.grade = grade

    • Student1 = Student("A",21,"A")
    • hasattr(Student1,"age")
    • hasattr(Student1,"marks")
    • setattr(Student1,"marks","75")
    • hasattr(Student1,"marks")
    • getattr(Student1,"grade")
    • getattr(Student1,"name")
    • delattr(Student1,"marks")
    • hasattr(Student1,"marks")

      code

Sothis article has basic of class and mainly I define the Built-In features of Class in python. Remaining concept of OOPS work the same as another Programming language.

Up Next
    Ebook Download
    View all
    Learn
    View all