Classes and Objects in Python
Classes and Objects in Python
class myClass():
Step 2) Inside classes, you can define functions or methods that are part of this class
Step 3) Everything in a class is indented, just like the code in the function, loop, if statement, etc.
Anything not indented is not in the class
• The self-argument refers to the object itself. Hence the use of the word self. So inside this
method, self will refer to the specific instance of this object that's being operated on.
• Self is the name preferred by convention by Pythons to indicate the first parameter of instance
methods in Python. It is part of the Python syntax to access members of objects
CMPINFOTECH pg. 1
Python Programming Classes and Objects CMP infotech
c = myClass()
c.method1()
c.method2("Testing is fun")
• Notice that when we call the method1 or method2, we don't have to supply the self-keyword.
That's automatically handled for us by the Python runtime.
• Python runtime will pass "self" value when you call an instance method on in instance, whether
you provide it deliberately or not
• You just have to care about the non-self arguments
def method2(self,someString):
print "Software Testing:" + someString
def main():
# exercise the class methods
c = myClass ()
c.method1()
c.method2(" Testing is fun")
if __name__== "__main__":
main()
CMPINFOTECH pg. 2
Python Programming Classes and Objects CMP infotech
class DerivedClass(BaseClass):
body_of_derived_class
def method2(self):
print ("Software Testing:")
class childClass(myClass):
def main():
# exercise the class methods
c2 = childClass()
c2.method1()
c2.method2()
if __name__== "__main__":
main()
Notice that the in childClass, method1 is not defined but it is derived from the parent myClass.
The output is "Guru99."
CMPINFOTECH pg. 3
Python Programming Classes and Objects CMP infotech
Now, the method 1 is defined in the childClass and output "childClass Method1" is correctly
shown.
ParentClass.Method.
Method 2 of the child class is called and "childClass method" is printed as expected.
Python Constructor
A constructor is a special type of method (function) which is used to initialize the instance
members of the class.
1. Parameterized Constructor
2. Non-parameterized Constructor
Constructor definition is executed when we create the object of this class. Constructors also
verify that there are enough resources for the object to perform any start-up task.
All classes have a function called __init__(), which is always executed when the
class is being initiated.
CMPINFOTECH pg. 4
Python Programming Classes and Objects CMP infotech
Example
1. class Employee:
2. def __init__(self,name,id):
3. self.id = id;
4. self.name = name;
5. def display (self):
6. print("ID: %d \nName: %s"%(self.id,self.name))
7. emp1 = Employee("John",101)
8. emp2 = Employee("David",102)
9. #accessing display() method to print employee 1 information
10. emp1.display();
11.
12. #accessing display() method to print employee 2 information
13. emp2.display();
Output:
ID: 101
Name: John
ID: 102
Name: David
Output:
CMPINFOTECH pg. 5
Python Programming Classes and Objects CMP infotech
Output:
Output:
Python Inheritance
Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides
code reusability to the program because we can use an existing class to create a new class
instead of creating it from scratch.
In inheritance, the child class acquires the properties and can access all the data members
and functions defined in the parent class. A child class can also provide its specific
implementation to the functions of the parent class. In this section of the tutorial, we will
discuss inheritance in detail.
In python, a derived class can inherit base class by just mentioning the base in the bracket
after the derived class name. Consider the following syntax to inherit a base class into the
derived class.
CMPINFOTECH pg. 6
Python Programming Classes and Objects CMP infotech
Syntax
1. class derived-class(base class):
2. <class-suite>
A class can inherit multiple classes by mentioning all of them inside the bracket. Consider
the following syntax.
Syntax
1. class derive-class(<base class 1>, <base class 2>, ..... <base class n>):
2. <class - suite>
Example 1
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #child class Dog inherits the base class Animal
5. class Dog(Animal):
6. def bark(self):
7. print("dog barking")
8. d = Dog()
9. d.bark()
10. d.speak()
Output:
dog barking
Animal Speaking
CMPINFOTECH pg. 7
Python Programming Classes and Objects CMP infotech
Syntax
1. class class1:
2. <class-suite>
3. class class2(class1):
4. <class suite>
5. class class3(class2):
6. <class suite>
7. .
8. .
CMPINFOTECH pg. 8
Python Programming Classes and Objects CMP infotech
Example
1. class Animal:
2. def speak(self):
3. print("Animal Speaking")
4. #The child class Dog inherits the base class Animal
5. class Dog(Animal):
6. def bark(self):
7. print("dog barking")
8. #The child class Dogchild inherits another child class Dog
9. class DogChild(Dog):
10. def eat(self):
11. print("Eating bread...")
12. d = DogChild()
13. d.bark()
14. d.speak()
15. d.eat()
Output:
dog barking
Animal Speaking
Eating bread...
CMPINFOTECH pg. 9
Python Programming Classes and Objects CMP infotech
Syntax
1. class Base1:
2. <class-suite>
3.
4. class Base2:
5. <class-suite>
6. .
7. .
8. .
9. class BaseN:
10. <class-suite>
11.
12. class Derived(Base1, Base2, ...... BaseN):
13. <class-suite>
Example
1. class Calculation1:
2. def Summation(self,a,b):
3. return a+b;
4. class Calculation2:
5. def Multiplication(self,a,b):
6. return a*b;
7. class Derived(Calculation1,Calculation2):
8. def Divide(self,a,b):
9. return a/b;
10. d = Derived()
11. print(d.Summation(10,20))
12. print(d.Multiplication(10,20))
13. print(d.Divide(10,20))
Output:
30
200
0.5
Example:
CMPINFOTECH pg. 10
Python Programming Classes and Objects CMP infotech
2 p = a*b
3 print(p)
6 p = a*b*c
7 print(p)
10 #product(2, 3)
11 product(2, 3, 5)
Output:
30
Output:
CMPINFOTECH pg. 11
Python Programming Classes and Objects CMP infotech
Output:
Method overloading is not supported in Python, because if we see in the above example we
have defined two functions with the same name ‘product’ but with a different number of
parameters.
But in Python, the latest defined will get updated, hence the function product(a,b) will
become useless.
Example:
CMPINFOTECH pg. 12
Python Programming Classes and Objects CMP infotech
1 class A:
2 def sayHi(self):
3 print(“I am in A”)
5 class B(A):
6 def sayHi(self):
7 print(“I am in B”)
9 ob = B()
10 ob.sayHi()
Output:
I am in B
Output:
CMPINFOTECH pg. 13