5) Classes

Python Classes

Like any other language such as javascript or PHP python allows you to write code in an object-oriented way. Classes are a great way to encapsulating all the functionality that can be grouped together.


Let us first look at the example by defining a class:

Inside the class, we can define functions, called as methods in OOPs. Usually, the first argument of any method inside a class defined by self argument, the self argument refers to the object itself. Inside the main class, we will instantiate the class.

c = myClass() will instantiate an object instance of this class.
Now we have created the class object we can call methods on the class

c.m1() and c.m2(“This is a given string”) calling m1 and m2 methods inside a class.
Note: When calling a function we do not need to pass self keyword that will automatically handle by python

Inherit a class from another class


cdClass method 1
cdClass method2 This is a given string
cdClass method 1
Second class method 1
cdClass method2 this is called form child class
second class method2 inside child class

Here we have defined a class cdClass and another class secondClass(cdClass ) inside second class we have passed name for cdClass as an argument that means secondClass is inheriting cdClass and all featured of cdClass will be reflected inside secondClass.