26
Python Classes and Objects
Python is an amazing programming language that supports both the functional programming paradigm and object-oriented programming paradigm.
Python's object-oriented programming system supports all the four fundamental features of a general OOPS framework: encapsulation, abstraction, inheritance and polymorphism.
Python's object-oriented programming system supports all the four fundamental features of a general OOPS framework: encapsulation, abstraction, inheritance and polymorphism.
To create a class, use the keyword class as shown below
class ClassName:
<statement-1>
<statement-N>
1.Class Objects
From python documentation;
Class objects support two kinds of operations: attribute references and instantiation.
Attribute references use the standard syntax used for all attribute references in Python: obj.name.
From python documentation;
Class objects support two kinds of operations: attribute references and instantiation.
Attribute references use the standard syntax used for all attribute references in Python: obj.name.
class Student:
"""A simple example class"""
rollno = 12
name = "Korir"
def messg(self):
return 'New Session will start soon.'
From the above example, Student.roll_no, Student.name are valid attribute references, returning an 12 and 'Korir' respectively.
Student.messg returns a function object.
In Python self is a name for the first argument of a method which is different from ordinary function. Rather than passing the object as a parameter in a method the word self refers to the object itself.
Instantiation uses function notation.
To create an instance of a class, you call the class as if it were a function. The example below creates a new instance of the class and assigns this object to the local variable x.
x = Student()
def __init__(self):
self.data = []
__ init __() method may have arguments for greater flexibility for example:
class Student:
"""A simple example class"""
def __init__(self,sroll, sname):
self.r = sroll
self.n = sname
x = Student(10, 'Korir')
x.r, x.n
Output
(10, 'Korir')
2.Instance Objects
Data attributes correspond to instance variables and need not be declared; like local variables, they spring into existence when they are first assigned to.
A method is a function that belongs to an object.
Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define corresponding methods of its instances.
Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define corresponding methods of its instances.
From our example;
x.messg is a valid method reference, since Student.messg is a function.
x.messg is a valid method reference, since Student.messg is a function.
3.Method Objects
x.messg()
Instance attribute
class Circle:
def __ init __(self, radius):
self.pi = 3.14159
self.radius = radius
def area(self):
return self.pi * self.radius**2
def circumference(self):
return 2*self.pi * self.radius
Class Attribute
class Circle:
pi = 3.14159
def __init__(self, radius):
self.radius = radius
def area(self):
return self.pi * self.radius**2
def circumference(self):
return 2 * self.pi * self.radius
object_name.class_attribute
class_name.class_attribute
From class attribute example;
c = Circle(10)
print(c.pi)
print(Circle.pi)
Output will be;
3.14159
3.14159
class DerivedClassName(BaseClassName):
<statement-1>
<statement-N>
class Parent():
def first(self):
print('first function')
class Child(Parent):
def second(self):
print('second function')
ob = Child()
ob.first()
ob.second()
Here's the output:
first function
second function
Sub-classing
Calling a constructor of the parent class by mentioning the parent class name in the declaration of the child class is known as sub-classing. A child class identifies its parent class by sub-classing.
Other types of inheritance are;
Multiple Inheritance-When a child class inherits from more than one parent class.
Multilevel Inheritance-When a child class becomes a parent class for another child class.
Hybrid Inheritance- involves multiple inheritance from the same base or parent class.
Hierarchical Inheritance-involves multiple inheritance taking place in a single program.
Multilevel Inheritance-When a child class becomes a parent class for another child class.
Hybrid Inheritance- involves multiple inheritance from the same base or parent class.
Hierarchical Inheritance-involves multiple inheritance taking place in a single program.
26