Python Object Oriented Programming Language

Python is a multi-programming language, as it supports different programming approach, in that the most popular approach is to solve a programming problem by creating an object; this is known as an Object-oriented programming language.

The Three Important terms of Oops concept are:

Class : A Class is nothing but a plan/blueprint/template/model.

Object : A Physical existence/An Instance of a class is called an Object.

Reference Variable : A variable that is used to refer to an object are called reference variables. The main purpose of the reference variable is to perform required operations on python objects.



Features of Python

Class

In Python, everything is an object. To create objects, we required some Model or Plan or Blueprint, which is nothing but a class.

  • We can write a class to represent some properties (attributes) and actions (behavior) of an object.
  • Properties can be represented by variables
  • Actions can be represented by Methods.
  • Hence class contains both variables and methods.
How to define a Class?

We can define a class by using the class keyword.

The Syntax for the class is:

class className:

 ''' documentation string '''

 variables:instance variables,static and local variables

methods: instance methods, static methods, class methods

The documentation string represents the description of the class. Within the class, a doc string is always optional. We can get a doc string by using the following two ways.

  • print(classname.__doc__): where __doc__ is a predefined variable that can be used to access the documentation string of the class.
  • help(classname)

Example:

class Student:
    '''This class developed by chercher.tech for demo purpose'''
    #variables
    #methods
print(Student.__doc__)

The output is:

accessing-student-documentation-string-of-class

We can also access the student class by using the help(student)

class Student:
    '''This class developed by chercher.tech for demo purpose'''
help(Student)

The output is:

accessing-student-by-using-help

Within the Python class, we can represent data by using variables

There are three types of variable are allowed :

  • Instance Variables (Object Level Variables)
  • Static Variables (Class Level Variables)
  • Local variables (Method Level Variables)

Within the python class, we can represent the operations by using the Methods; The following are the various types of methods available in python :

  • Instance Method
  • Class Method
  • Static Method

The following example demonstrates how to access the class and how to define methods and how to call a method by using class

class Student:
    '''This class developed by chercher.tech for demo purpose'''
    def __init__(self):
        self.name="chercher.tech"     ###Defining a Method
        self.rollno=101
        self.marks=90
        
    def talk(self):
        print("Hello my Name is:",self.name)
        print("My Roll is:",self.rollno)        ##Defining another Method
        print("My Marks are:", self.marks)
        
s=Student() ## Defining a  reference variable
print(s.name)   ## by using reference variable accessing the propertiese of an object
print(s.rollno)   ## by using reference variable accessing the propertiese of an object
print(s.marks)   ## by using reference variable accessing the propertiese of an object
s.talk()     #### by using reference variable calling a method

The output is:

output-of-demo-program-of-class-and-methods

The following example demonstrates how to define the class and how to define methods and how to call methods.

class Student:
    def __init__(self,name,rollno,marks):
        self.name=name
        self.rollno=rollno
        self.marks=marks
        
    def talk(self):
        print("Hello my Name is:",self.name)
        print("My Roll is:",self.rollno) 
        print("My Marks are:", self.marks)
        
s1=Student("Atharva",80,45)
s2=Student("Dhanu",67,93)
s1.talk()
s2.talk()

The output is:

accesing-methods-and-objects

Working with IDLE Python

Self Variable

The Self is the default variable which is always pointing to the current object (like this keyword in Java)

By using self, we can access instance variables and instance methods of the object.

We should use the self within the python class to refer to the current object.

1) self should be the first parameter inside the constructor: def __init__(self):
2) self should be the first parameter inside instance methods: def talk(self):

The following example demonstrates the use of a self variable in the python program

Let us consider, we have created a class called test and inside this test class we are defining a __init__ () the method by using a self variable.

And also I am creating an object by using T, we can assign a Test object to T outside of the class and hence to access this object inside the class, we are using the self variable.

Now, Self and T both are pointing to the same object, let us prove this with the following example:

class Test:
    def __init__(self):
        print("The address of an object pointing by self variable:",id(self))
T=Test()
print("The address of an object poiting by T:",id(T))

The output is:

self-variable-and-obect-poiniting-to-the-same-object

The following example demonstrates the constructor in the class

Consider a class that contains one constructor, and one method, The first argument to the constructor and to the instance method is always self.

Now, let us create an object; so whenever we create an object automatically the constructor will be called but we are not passing any value to the self argument from here because the python is responsible for assigning a value to self and instance variables.

But the instance method is having two arguments such as self and x, we only required to assign a value to x.

class Test:
    def __init__(self):            ##constructor
        print("Constructer")
    def m1(self,x):               ##Method
        print("x value is:",x)
T=Test()
T.m1(10)

The output is:

assigning-vaue-to-instance-variabe

Python is not a keyword. Instead, you can use any other keyword, but preferred to use self

class Test:
    def __init__(kelf):
        print("Constructor")
    def m1(kelf,x):
        print("x value is:",x)
T=Test()
T.m1(10)

The output is:

kef-instaed-of-self

The first argument whatever we are passing is treated as a self argument, and python is responsible for assigning values to it.

class Student:
##here the name argument is treated as a self argument and python is going to assign a value to it
    def __init__(name, roll no, marks):  
        self.name=name
        self.rollno=rollno
        self.marks=marks
        
    def talk(self):
        print("Hello my Name is:",self.name)
        print("My Roll is:",self.rollno) 
        print("My Marks are:", self.marks)
/*here we are passing 3 values including name, but python treats the name as a self variable and try to assign a value then we will get
     a type error*/      
s1=Student("Atharva",80,45) 
s2=Student("Dhanu",67,93)                                                                 
s1.talk()
s2.talk()

The output is:

type-error-of-positional-arguments

The important points to be Remember :

  • Within the python, class to refer to the current object, some variables must be required, which is nothing but self.
  • The self is a reference variable, always pointing to the current object.
  • The first argument to the constructor and instance method is self
  • At the time of calling the constructor or instance method, we are not required to pass any value to a self variable, internally the python virtual machine is responsible to provide value.
  • The main purpose of the self variable within the class is to declare an instance variable and access the values of instance variables.
  • The self is not a keyword. Instead, we can use any other key but recommended to use self.
  • We cannot use self outside the class.
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions